0

Basically I have an main object dataToMovie, I have empty arrays. I want to display the content of the arr variable into mov[] and the content of the arr2 into ser[]. I have attempted to do something as you can see. I am looking to do this seperately for each array as I will have multiple data in the future

const dataToMovie = {
  createMovie,
  links: {
    mov: [],
    ser: [],
    rev: [],
    ext: [],
  },
};

const dataToInsert1 = {
  'model-10389': 164703,
  'model-10388': 164704,
  'model-10387': 164705,
};

const dataToInsert2 = {
  'model-10389': [1656, 1234, 1245],
  'model-10384': [1656, 1234, 1245],
  'model-10383': [1656, 1234, 1245],
};

const arr = Object.entries(dataToInsert1).map((entry) => ({
  id: entry[0].substring(6, entry[0].length),
  value: entry[1],
}));

//dataToMovie.links.mov[arr]

const arr2 = Object.entries(dataToInsert2).map(([key, value]) => ({
  modelId: key.substring(6),
  ids: value,
}));

//dataToMovie.links.ser[arr2]
2
  • 1
    you can use concat: dataToMovie.links.mov.concat(arr); Commented Jul 1, 2022 at 12:44
  • Arrays do have a .push() method to add things to them. You can even immediately push to it inside a foreach loop instead of mapping, if preferred. Commented Jul 1, 2022 at 12:48

2 Answers 2

1

concat is a good candidate for this operation. it returns a merged array.

dataToMovie.links.mov = dataToMovie.links.mov.concat( arr )
dataToMovie.links.ser = dataToMovie.links.set.concat( arr2 )
Sign up to request clarification or add additional context in comments.

Comments

0

You are close. if the arrays are empty you can just do:

dataToMovie.links.mov = arr
dataToMovie.links.ser= arr2

If the arrays have items in them and you just want to add to them, you can use the spread operator

dataToMovie.links.mov = [...dataToMovie.links.mov, ...arr]
dataToMovie.links.ser = [...dataToMovie.links.ser, ...arr2]

1 Comment

I have a question, basically when I do dataToMovie.links.mov = arr is it assigning "arr" to the mov[] it seems like it is recreating a new object each time with the links object and mov array. is this recreating a new object ? I am looking only to insert the data inside, not recreating everything all the time. Do you see what I mean

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.