13

I have a problem when get item in sub array and put back to main array by javascript. I have array like this:

var array_demo = [
    ['1st', '1595', '8886'],
    ['2nd', '1112']
]

I want to get result like this: ['1595','8886','1112']

But when I use this code:

array_demo.map(function(i) {
    return i.slice(1).join();
});

Result: ['1595,8886', '1112']

Can someone help me ?

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
Nghia Phan
  • 191
  • 2
  • 10

7 Answers7

12

You could destructure the array and take the rest without the first element and flatten this array using Array.flatMap.

var array = [['1st', '1595', '8886'], ['2nd', '1112']],
    result = array.flatMap(([_, ...a]) => a);

console.log(result);

Alternatively Array.slice() works as well.

var array = [['1st', '1595', '8886'], ['2nd', '1112']],
    result = array.flatMap(a => a.slice(1));

console.log(result);
Harun Yilmaz
  • 8,281
  • 3
  • 24
  • 35
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
5

Instead of join() use Array.prototype.flat() on the returned result of map():

var array_demo = [
    ['1st', '1595', '8886'],
    ['2nd', '1112']
]
array_demo = array_demo.map(function(i) {
    return i.slice(1);
}).flat();
console.log(array_demo);

In one liner using Arrow function(=>):

var array_demo = [
    ['1st', '1595', '8886'],
    ['2nd', '1112']
]
array_demo = array_demo.map(i => i.slice(1)).flat();
console.log(array_demo);
Mamun
  • 66,969
  • 9
  • 47
  • 59
3

You can use Array.prototype.flat followed by a Array.prototype.filter to get rid of the non-numeric data in your subarrays:

var array_demo = [
    ['1st', '1595', '8886'],
    ['2nd', '1112']
];

const res = array_demo.flat().filter(n=> !isNaN(n));
console.log(res);

We could do it using Array.prototype.reduce also, if your objective is to remove only the object at the 0th index:

var array_demo = [
    ['1st', '1595', '8886'],
    ['2nd', '1112']
];

const res = array_demo.reduce((r, [_, ...e]) => { r.push(...e); return r}, []);
console.log(res);
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
  • Thanks for your help, it working very well, but too much right answer, I don't know where is the best so I choose the faster anwser. Finally, thanks very much. – Nghia Phan Sep 18 '19 at 08:30
1

You could also use array.reduce, and use it as follows:

array_demo.reduce( function(prev, curr, index) {
  return prev.concat(curr.slice(1)); 
} ).slice(1);

Thing is, array.map, returns the result for each element in the array. So, in your example, it has two cycles:

  1. ['1st', '1595', '8886'] -> returns '1595,8886' because you used join()
  2. ['2nd', '1112'] -> returns '1112' also because you used join()

And you get that in an array -> ['1595,8886', '1112'].

dkasipovic
  • 5,930
  • 1
  • 19
  • 25
1

array_demo.reduce((acc, array_element) => acc.concat(array_element.slice(1)), [])

Dejan
  • 353
  • 2
  • 12
  • Thanks for your answer, but very very sorry because I can't get you the green tick, have too much right answer. Finally, thanks so much because take your time to help me. :) – Nghia Phan Sep 18 '19 at 08:40
  • No worries, just pick the one that's cleanest ;) – Dejan Sep 18 '19 at 08:42
1

Use Array.prototype.reduce()

var array_demo = [
    ['1st', '1595', '8886'],
    ['2nd', '1112']
]

var reduced_array = array_demo.reduce((reducedArray, elem) => {
  elem.shift() // Remove the first item
  return reducedArray.concat(elem)
}, [])

console.log(reduced_array)
UtkarshPramodGupta
  • 7,486
  • 7
  • 30
  • 54
1

when we need a single accumulated values, we can use reduce funcions.

var array_demo = [
    ['1st', '1595', '8886'],
    ['2nd', '1112']
]

var l = array_demo.reduce((val, item)=> {
    return val.concat(item.slice(1));
},[]);

console.log(l);
Nitin Shekhar
  • 175
  • 2
  • 15