0

I have this 2 array of objects like this:

objects [ { countMedias: 2 },
  { countMedias: 1 },
  { countMedias: 3 },
  { countMedias: 1 },
  { countMedias: 2 } ]
listePlayliste [ { nom_playlist: 'bbbb' },
  { nom_playlist: 'ccc' },
  { nom_playlist: 'aaaa' },
  { nom_playlist: 'xxxx' },
  { nom_playlist: 'resttttttttt' } ]

I want to fusion both of them to get something like this :

Result [ { nom_playlist: 'bbbb', countMedias: 2 },
  { nom_playlist: 'ccc', countMedias: 1  },
  { nom_playlist: 'aaaa', countMedias: 3 },
  { nom_playlist: 'xxxx', countMedias: 1 },
  { nom_playlist: 'resttttttttt', countMedias: 2 } ]

I tried this but it is not what I want actually:

    Array.prototype.push.apply(json,objects); 
0

3 Answers 3

2

Maybe something like this:

objects.map((object, index) => Object.assign(object, listePlayliste[index]))

Don't try to use this on a large array size though. It's not that fast.

Sign up to request clarification or add additional context in comments.

1 Comment

Just be sure that you are fine modifying the target object. Otherwise you would need instead Object.assign({}, object, listePlayliste[index]) in order to not modify the original object
1

use map. Iterate over one of the array and return the object, which holds the current obj and object from other array using index.

const objects = [{
    countMedias: 2
  },
  {
    countMedias: 1
  },
  {
    countMedias: 3
  },
  {
    countMedias: 1
  },
  {
    countMedias: 2
  }
];

const listePlayliste = [{
    nom_playlist: 'bbbb'
  },
  {
    nom_playlist: 'ccc'
  },
  {
    nom_playlist: 'aaaa'
  },
  {
    nom_playlist: 'xxxx'
  },
  {
    nom_playlist: 'resttttttttt'
  }
];

const output = objects.map((obj, i) => ({
  ...obj,
  ...listePlayliste[i]
}));
console.log(output);

Comments

0

Similar to objects.map(), a for loop will also work, but please be noted that, Object.assign() will modify the original array. PFB code snippet:

objects = [ { countMedias: 2 },
  { countMedias: 1 },
  { countMedias: 3 },
  { countMedias: 1 },
  { countMedias: 2 } ];
listePlayliste = [ { nom_playlist: 'bbbb' },
  { nom_playlist: 'ccc' },
  { nom_playlist: 'aaaa' },
  { nom_playlist: 'xxxx' },
  { nom_playlist: 'resttttttttt' } ];


for (let i=0; i<objects.length; i++) {
  Object.assign(objects[i], listePlayliste[i]);
}

console.log(objects);

To avoid modifying original array, we can use Spread syntax. PFB code snippet:

objects = [ { countMedias: 2 },
  { countMedias: 1 },
  { countMedias: 3 },
  { countMedias: 1 },
  { countMedias: 2 } ];
listePlayliste = [ { nom_playlist: 'bbbb' },
  { nom_playlist: 'ccc' },
  { nom_playlist: 'aaaa' },
  { nom_playlist: 'xxxx' },
  { nom_playlist: 'resttttttttt' } ];

var result = [];
for (let i=0; i<objects.length; i++) {
  result.push({ ...objects[i], ...listePlayliste[i] })
}

console.log(result);
console.log(objects);

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.