-1

I have an array of arrays of objects like this:

var array = [
  [
    {
        name: 'a',
        value: 1,
        where: '1st array'
    },
    {
        name: 'b',
        value: 2,
        where: '1st array'
    }
  ],
  [
    {
        name: 'a',
        value: 1,
        where: '2nd array'
    },
    {
        name: 'b',
        value: 2,
        where: '2nd array'
    }
  ]
]

And I want to convert it to this:

[
  ['a', 1, '1st array'],
  ['b', 2, '1st array'],
  ['a', 1, '2nd array'],
  ['b', 2, '2nd array']
]

Can this be done using the array.map() method? I'm asking because there can be more than 1000 objects/array that will have to be converted and I think that a simple for inside for might not be efficient...

Valip
  • 3,500
  • 5
  • 47
  • 109
  • If execution time is critical you could write both solution and test which is the fastest. I think `for` will fare better than you might think. – KIKO Software Dec 08 '17 at 09:06
  • Have a look here: https://stackoverflow.com/questions/20881213/converting-json-object-into-javascript-array – OlafW Dec 08 '17 at 09:06
  • Looping through 1000 elements is not going to be slow, regardless of how you do it. – JJJ Dec 08 '17 at 09:07
  • @KIKOSoftware How can I do `array.map()` for nested arrays, like in my example? – Valip Dec 08 '17 at 09:10
  • You will need to loop anyways. Unless you want to pick up data at specific indexes without iteration. It is just O(N) anyways, – gurvinder372 Dec 08 '17 at 09:10
  • @gurvinder372 so I will have to have a `for` first and then call `array.map()` inside it ... that's what you're saying ? – Valip Dec 08 '17 at 09:11
  • No, a simple map will do. – gurvinder372 Dec 08 '17 at 09:11
  • What you need is more or less a `flatMap` which you can see discussed in JS here https://gist.github.com/samgiles/762ee337dff48623e729 and here https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays-in-javascript – timothyclifford Dec 08 '17 at 09:13

4 Answers4

2

Use simple for loop for this with Object.values method, to get values as array

var arr = [
  [
    {
        name: 'a',
        value: 1,
        where: '1st array'
    },
    {
        name: 'b',
        value: 2,
        where: '1st array'
    }
  ],
  [
    {
        name: 'a',
        value: 1,
        where: '2nd array'
    },
    {
        name: 'b',
        value: 2,
        where: '2nd array'
    }
  ]
];
var newArr = [];
for(let i in arr){
  for(let j in arr[i]){
    newArr.push(Object.values(arr[i][j]));
  }
}
console.log(newArr);
Niklesh Raut
  • 29,238
  • 9
  • 61
  • 94
1

You could use a destruction assignment for the inner array.

var array = [[{ name: 'a', value: 1, where: '1st array' }, { name: 'b', value: 2, where: '1st array' }], [{ name: 'a', value: 1, where: '2nd array' }, { name: 'b', value: 2, where: '2nd array' }]],
    result = array.reduce(
        (r, a) => r.concat(a.map(({ name, value, where }) => ([name, value, where]))),
        []
    );
    
console.log(result);
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324
0

Here is another way to achieve this. Get values like values=Object.values(array) and iterate over these values using simple for loop.

var array = [
  [
    {
        name: 'a',
        value: 1,
        where: '1st array'
    },
    {
        name: 'b',
        value: 2,
        where: '1st array'
    }
  ],
  [
    {
        name: 'a',
        value: 1,
        where: '2nd array'
    },
    {
        name: 'b',
        value: 2,
        where: '2nd array'
    }
  ]
]
var outPut =[]
var values=Object.values(array);
//console.log(values);
for(var i=0;i<values.length;i++){
 for(var j=0;j<values[i].length;j++){
   //console.log(values[i][j]);
   outPut.push(Object.values(values[i][j]));
  
 }
}
console.log(outPut);
manikant gautam
  • 3,109
  • 1
  • 15
  • 24
0

You need to do this in following way... you could improve it If you don't want to make it static , and you can make it dynamic. but it will be lengthy code ... :) var object1,object2;

array[0].forEach(function(element,index){
   obejct1[index] = Object.values(element);      

});

array[1].forEach(function(element,index){
   obejct2[index] = Object.values(element);      

});

object1[0].concat(object1[1],object2[0],object1[1]);