Home > Enterprise >  JavaScript - Creating Object with specific format out of 2 Arrays
JavaScript - Creating Object with specific format out of 2 Arrays

Time:12-22

I have 2 Arrays. The Array "people" just holds basic information of some people. The Array "subscriptions" has a lot of different subscriptions to games. I want to have have an Array, where I can sort of have an Overview of what game subscriptions each person has. This is not real code I use, I am just trying to get used to JavaScript.

an example Element of an Array called people:

{"number":4251,"name":"Alex","surname":"Scott"}

an example Element of an Array called subscriptions:

{"number":4329,game:"Tetris"}

I want to make a new new Array with the following format:

person: (people[i]), subscriptions: [(subscriptions[j], subscriptions[j k], ...)]

What I tried:

const array3 = people.map(x => {"person": x , "subscriptions": subscriptions.filter(y => y.number === x.number)});

It get this Error:

SyntaxError: Unexpected token :

How can I insert multiple key value pairs in in these Objects?

CodePudding user response:

This happens because your argument in the map is interperting the { as opening bracket of the method body.

const arr = [{some: 1, object: 2}, {some:3, object:4}]
arr.map((o) => {original: o, cool: 'yes'})

To resolve this you have to explicitly return the object or add additional parenthesis to let the interperter know that this is not the method body:

const arr = [{some: 1, object: 2}, {some:3, object:4}]
const mapped = arr.map((o) => {return {original: o, cool: 'yes'}})
console.log(mapped)

const arr = [{some: 1, object: 2}, {some:3, object:4}]
const mapped = arr.map((o) => ({original: o, cool: 'yes'}))
console.log(mapped)

I hope this helps!

CodePudding user response:

In your original code there is a syntax error, when it comes to the mapping part. Either you go with the long version of this command or, since you directly want to return the element, you can use the short version:

const people = [{"number":4251,"name":"Alex","surname":"Scott"}, {"number": 4329, "name":"Mr", "surname": "Tetri"}]

const subscriptions = [{"number":4329,game:"Tetris"}, {"number":4329, game:"Solitaire"}, {number: 4251, game: "Tetris"}]

// using an anonymous function including full body
const arrayLongVersion = people.map(x => { return {"person": x , "subscriptions": subscriptions.filter(y => y.number === x.number)} });

// using an anonymous function with direct return of elements
const arrayShortVersion = people.map(x => ({person: x , subscriptions: subscriptions.filter(y => y.number === x.number)}))

CodePudding user response:

Here you go

const people = [
  {"number":1,"name":"Alex","surname":"Scott"},
  {"number":2,"name":"John","surname":"Anderson"},
  {"number":3,"name":"Jacob","surname":"Sanderson"},
  {"number":4,"name":"Lionel","surname":"Messi"},
  {"number":5,"name":"Cristiano","surname":"Ronaldo"}
];

const subscriptions = [
  {"number":1,game:"Tetris"},
  {"number":2,game:"Super Mario"},
  {"number":3,game:"Fortnite"},
  {"number":4,game:"FIFA"},
  {"number":5,game:"Zelda"}
];

const peopleSubscriptions = people.map(person => {
  return {
    ...person,
    subscriptions: subscriptions.filter(sub => sub.number === person.number)
  }
});

console.log(peopleSubscriptions);

  • Related