1

I have an array looking like this: [Screenshot]

   ▼[Array(5)]
       ▼0: Array(5)
            0: pfQ1A3M0S1V6icGcLKTIdBoIWA42
            1: 0rrfZUw6RAcSxoAknCJJ6GGgB9y1
            2: 6RXWFEsEMdZWJTeUN3wMAj1PzYu2
            3: QK39c753XUP7MbqVbMObSYPdmKB2
            4: r8sN2eYtBTSvFSVunQQ71nxAGbO2
            length: 5
           ▶__proto__: Array(0)
        length: 1
       ▶__proto_: Array(0)

   ▼(2) [Array(5), "XG9oznIXpucqPFekDrGm6ixpctt2"]
       ▶0: (5) ["0rrfZUw6RAcSxoAknCJJ6GGgB9y1", "0rrfZUw6RAcSxoAknCJJ6GGgB9y1", …] // From first array
        1: "XG9oznIXpucqPFekDrGm6ixpctt2"
        length: 2
       ▶__proto:__ Array(0)

This is my code:

this.old = this.checFilter.map(r=>r.userId);
this.checked.push(this.old);
console.log(this.checked);

As you can see, I have 5 arrays in first array. And then I am pushing it to an other array. Result is showing 2 arrays after push. But I need to have an array with 6 entries as a result.

4
  • 1
    Please provide your variables as text, not as images. Commented Jan 27, 2020 at 13:08
  • 1
    You are pushing the new value on the wrong level … Commented Jan 27, 2020 at 13:08
  • You do not have 5 arrays in the first array. You have an array with 5 element. I'm assuming you want 6 element in your array ? Commented Jan 27, 2020 at 13:10
  • You can use concat Commented Jan 27, 2020 at 13:10

3 Answers 3

1

You must use concat function:

https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Array/concat

this.old = this.checFilter.map(r=>r.userId);
this.checked = this.checked.concat(this.old);
console.log(this.checked);
Sign up to request clarification or add additional context in comments.

Comments

0

Use this:

This pushes each value directly into the array instead of the whole array as 1 value, but be careful. This way of doing it mutates your array.

this.checFilter.map(r => this.checked.push(r.userId));

1 Comment

Please add some explanation with your answer , so that others can learn as well.
0

Try to use flatMap() to flat your arrays:

arr.flatMap(f=> f);

An example:

let arr = [

  ['pfQ1A3M0S1V6icGcLKTIdBoIWA42',
    '0rrfZUw6RAcSxoAknCJJ6GGgB9y1',
    '6RXWFEsEMdZWJTeUN3wMAj1PzYu2',
    'QK39c753XUP7MbqVbMObSYPdmKB2',
    'r8sN2eYtBTSvFSVunQQ71nxAGbO2',],

  'XG9oznIXpucqPFekDrGm6ixpctt2'
]

const result = arr.flatMap(f=> f);
console.log(result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.