简体   繁体   English

如何在对象数组中为每个 object 添加带有值的键?

[英]How to add the key with value for each object inside an array of objects?

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?我有这个对象数组,我想在下面添加 object 的值,顺序与它在 items 数组中的顺序相同,有什么建议吗?

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? object 或数组,哪个更容易?

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

Expected output:预期 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)

Like this, assuming the uris are in an array像这样,假设uris在一个数组中

 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)

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.在我看来,您的obj常量应该是一个数组,因为您在那里没有任何键,并且假设您的items的顺序相同,这应该足够了。

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

Make obj an array使 obj 成为一个数组

 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在对象数组中分离每个对象的键和值-Javascript - How to separate key and value of each object inside array of objects - Javascript 如何根据每个对象id为数组中的每个对象添加一个密钥对值? - How to add to each object in an array a key-pair value based on each objects id? 如何将每个键对从此对象添加到数组中作为新对象? - How to add each key pair from this Object into an Array as new Objects? 如何从所有对象中删除一个简单的特定键值对并添加到数组内的一个特定 object - How to remove a simple specific key value pair from all objects and add to one specific object inside an array 如何对相同键的值求和并在每个对象的对象数组中添加一个键,该键的值包含 id 相同键的数组? - how to sum value of same keys and add a key with value that contains array of id same keys, in array of objects for each object? 删除对象数组中的对象键和值 - Remove object key and value inside of Array of Objects 如何根据每个 object 的日期值在对象数组中添加值 - How to add the values ​in an array of objects depending on the date value of each object TypeScript-如何将“键值”对添加到数组中的每个对象? - TypeScript- How to add a Key Value pair to each object in an Array? Angular 2为每个数组对象添加键和值 - Angular 2 add key and value to each array object 为巨大的深度嵌套对象中的每个对象添加新的键/值 - add new key/value to each object inside a huge deeply nested objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM