0

I have this array of objects and I want to add the value of the object below in the same order as it is inside the items array, Any suggestion?

const items = [{
    tshirt: "Model TS",
    jeans: "ModelXW",
  },
  {
    sneakers: "indcdsc54",
    furniture: "Table31S"
  },
  {
    dress: "indc54",
    short: "shortS2"
  },
];

either an object or an array, which one should be easier?

const obj = [
  "localhost:8080.com",
  "localhost:3000.com",
  "localhost:7000.com",
]

Expected output:

const items = [{
    tshirt: "Model TS",
    jeans: "ModelXW",
    website: "localhost:8080.com",
  },
  {
    sneakers: "indcdsc54",
    furniture: "Table31S",
    website: "localhost:3000.com",
  },
  {
    dress: "indc54",
    short: "shortS2",
    website: "localhost:7000.com",
  },
];

I have tried this way with no success, any suggestion?

const items = [{
    tshirt: "Model TS",
    jeans: "ModelXW"
  },
  {
    sneakers: "indcdsc54",
    furniture: "Table31S"
  },
  {
    dress: "indc54",
    short: "shortS2"
  }
];

const obj = [
  "localhost:8080.com",
  "localhost:3000.com",
  "localhost:7000.com",
]

let newArray = obj.map(uri => items.map(i => i["website"] = uri ))

console.log(newArray)

2
  • 1
    I would assume obj should be an array as it currently throws a syntax error. Plus objects do not have a .map method Commented Feb 26, 2021 at 15:29
  • My bad obj is an array :) @evolutionxbox Commented Feb 26, 2021 at 15:33

3 Answers 3

3

Like this, assuming the uris are in an array

const items = [{
    tshirt: "Model TS",
    jeans: "ModelXW"
  },
  {
    sneakers: "indcdsc54",
    furniture: "Table31S"
  },
  {
    dress: "indc54",
    short: "shortS2"
  }
];

const uris = [
  "localhost:8080.com",
  "localhost:3000.com",
  "localhost:7000.com",
]

let newArray = items.map((item,i) => (item.website = uris[i], item)); 
// OR          items.map((item,i) => ({website : uris[i], ...item}));
console.log(newArray)

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

4 Comments

I have two versions. Did you see them both? Interesting ways, comma operator or object spread
Yeah, For the first option what does the item mean " uris[i] , item" in this case?
I set the website and return what’s after the comma
Gotcha like saying pass the rest of the values of the object. thanks! :)
1

The way I see it your obj constant should be an array since you don't have any keys in there and assuming that your items are in the same order this should be enough.

const newArray = items.map((item,index) => {
  item.website = obj[index];
  return item
})

Comments

1

Make obj an array

const obj = [
  "localhost:8080.com",
  "localhost:3000.com",
  "localhost:7000.com",
]
const items = [{
    tshirt: "Model TS",
    jeans: "ModelXW",
  },
  {
    sneakers: "indcdsc54",
    furniture: "Table31S"
  },
  {
    dress: "indc54",
    short: "shortS2"
  },
];


obj.forEach((item, index) => {
  if (index < items.length) items[index].website = item;
})

console.log(items)

Comments

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.