3

The problem I have is, that I want to combine a 2D Array like this:

[
    [ "Renault", 61, 16, … ],
    [ "Ferrari", 58, 10, … ],
    [ "Mercedes", 32, 12, … ],
    [ "Mercedes", 24, 21, … ],
    [ "Toro Rosso", 7, 8, … ]
]

So in this example I have the String "Mercedes" twice at index 0 in the arrays. What I want the output to be is the following:

[
    [ "Renault", 61, 16, … ],
    [ "Ferrari", 58, 10, … ],
    [ "Mercedes", 56, 33, … ],
    [ "Toro Rosso", 7, 8, … ]
]

So if a String like "Mercedes" is appearing twice in that array I want the second one to be deleted. But the values from the second array must be taken over into the first "Mercedes" Array.

1
  • And what have you tried to achieve the desired results? This is not a code writing service, so we expect you to show what you've done already. Please read the help center and the How to Ask section in particular
    – freefaller
    Jul 2, 2019 at 15:14

4 Answers 4

2

It seems that you actually want to aggregate similar values in the array. Therefore you could do a cycle in foreach and save yourself in a temporary array the string values already known and instead in another your merged classes. At that point, as you scroll through your array, you can check if you have already added an equal string in the first fake, and if so, you can take its index and aggregate them correctly in the other array.

let arr = [
  ["Renault", 61, 16],
  ["Ferrari", 58, 10],
  ["Mercedes", 32, 12],
  ["Mercedes", 24, 21],
  ["Toro Rosso", 7, 8]
]
var tmp1 = [], tmp2 = [];
arr.forEach(function(currentValue, index, arr) {
    // currentValue contain your element
    // index contain the index of your element
    // arr contain you entire "values" array
    var ind = tmp1.indexOf(currentValue[0]);
    if(ind === -1) {
        tmp1.push(currentValue[0]);
        tmp2.push(currentValue);
    } else {
        tmp2[ind][1] += currentValue[1];
        tmp2[ind][2] += currentValue[2];
    }
});
console.log(tmp2);

2

You can use reduce() to build a object and then use Object.values()

const arr = [
    [ "Renault", 61, 16 ],
    [ "Ferrari", 58, 10],
    [ "Mercedes", 32, 12],
    [ "Mercedes", 24, 21],
    [ "Toro Rosso", 7, 8]
]

let res = arr.reduce((ac, [k, ...rest]) => {
  if(!ac[k]) ac[k] = [];
  ac[k] = ac[k].concat(k,...rest);
  return ac;
},[])
console.log(Object.values(res))

1

Sure, by creating an array for your names and then separate the unique ones from them, at last, match them with the index.

let arr = [
  ["Renault", 61, 16],
  ["Ferrari", 58, 10],
  ["Mercedes", 32, 12],
  ["Mercedes", 24, 21],
  ["Toro Rosso", 7, 8]
]
let c = [...new Set(arr.map(a => a[0]))] // get unique values of all arr[0]
let ans = arr.filter((a, post) => { 
  if (c[post]) { // check if index exists because we seperated one, the last is undefined here
    if (a[0] == c[post]) {
      return a;
    }
  } else {
    return a; // else return as usual
  }
})
console.log(ans)

You get the filtered array.

0

You can iterate over the array and keep a map object of the names you already processed with their index in the result array so you can access them quicker when you need to push more values.

i.e:

{
 "Renault": 0,
  "Ferrari": 1,
  "Mercedes": 2,
  "ToroRosso": 3
}

Here is a running example with the steps:

const arr = [
  ["Renault", 61, 16],
  ["Ferrari", 58, 10],
  ["Mercedes", 32, 12],
  ["Mercedes", 24, 21],
  ["Toro Rosso", 7, 8]
];

let map = {};
let globalIndex = 0;
let resultArray = [];
arr.forEach((currentArray) => {
  const [name, ...rest] = currentArray;
  if (map[name] === undefined) {
    // if our map doesn't already store this value
    // then store it with it's current index
    map[name] = globalIndex;
    // push it to the result array
    resultArray[globalIndex] = currentArray;
    // increment the global index 
    //so we can keep track of it in the next iteration
    globalIndex++;
  } else {
    // we already have an array with this name
    // grab the relevant index we previously stored
    // and push the items without the name via this index
    const relevantIdx = map[name];
    resultArray[relevantIdx] = [
      ...resultArray[relevantIdx],
      ...rest
    ]
  }
})

console.log(resultArray);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.