简体   繁体   English

如何将 object 数组中的数据添加到另一个 object 数组中? javascript

[英]How to add data from array of object with array inside to another array of object? javascript

This is my code, I want to create another filtered array like the example below, I have 2 arrays and want to add score information to it, I know it's simple but can't find the solution这是我的代码,我想创建另一个过滤数组,如下例所示,我有 2 个 arrays 并想向其中添加分数信息,我知道这很简单但找不到解决方案

  const wishesData = [
    {
       name: "Peter",
      presents: ["coffee", "holidays"]
    },
    {
       name: "Mario",
      presents: ["coffee", "videogames"]
    },
    {
       name: "Amanda",
      presents: ["computer", "tattoo"]
    }
  ]
  const scoresData= [
    {
      name: "Peter",
      score: 10
    },
    {
      name: "Mario",
      score: 2.3
    },
    {
       name: "Amanda",
      score: 1.1
    }
  ]
  const result = wishesData.map((ele) => {
      return {
               ...ele,
              score:  scoresData.find(s=> s.name === ele.name? s.score: 0)
                  }
  })
           console.log("este es el resultado=>",result)

I want to modify the array wishesData adding "score" to all objects inside and get to look like this example:我想修改数组wishData,将“分数”添加到里面的所有对象,并看起来像这个例子:

{
  name: "Mario",
  presents: ["coffee", "videogames"],
  score: 2.3
}

Please check the example and correction and suggestions.请检查示例和更正和建议。

  • Correction: scoresData.find(s=> s.name === ele.name? s.score: 0) - here you do not close bracket for Array.find and try to access it's property within find .更正: scoresData.find(s=> s.name === ele.name? s.score: 0) - 在这里你没有关闭Array.find的括号并尝试在find中访问它的属性。 In your code, you will get scoreData object instead of score .在您的代码中,您将获得scoreData object 而不是score
const match = scoresData.find(s=> s.name === ele.name); // -> ? s.score: 0)
return match ? match.score : 0;

// or simply
const score = scoresData.find(s=> s.name === ele.name)?.score || 0;
  • Suggestion: it will take O(N^2) time.建议:需要 O(N^2) 时间。 All iteration for wishesData need another iteration scoresData for each. wishesData的所有迭代都需要另一个迭代scoresData Why don't you use reduce provided in example?为什么不使用示例中提供的reduce
const scoreMap = scoresData.reduce((a, c) => ({
    ...a,
    [c.name]: c.score
}), {})

// you can easy to find score by
const result = wishesData.map((ele) => {
    return {
        ...ele,
        score: scoreMap[ele.name] || 0,
    }
})

Thanks谢谢

 const wishesData = [{ name: "Peter", presents: ["coffee", "holidays"] }, { name: "Mario", presents: ["coffee", "videogames"] }, { name: "Amanda", presents: ["computer", "tattoo"] } ] const scoresData = [{ name: "Peter", score: 10 }, { name: "Mario", score: 2.3 }, { name: "Amanda", score: 1.1 } ] const scoreMap = scoresData.reduce((a, c) => ({...a, [c.name]: c.score }), {}) const result = wishesData.map((ele) => { return {...ele, score: scoreMap[ele.name] || 0, } }) console.log("este es el resultado=>", result)

And this is just editing of your origin code这只是编辑您的原始代码

 const wishesData = [{ name: "Peter", presents: ["coffee", "holidays"] }, { name: "Mario", presents: ["coffee", "videogames"] }, { name: "Amanda", presents: ["computer", "tattoo"] } ] const scoresData = [{ name: "Peter", score: 10 }, { name: "Mario", score: 2.3 }, { name: "Amanda", score: 1.1 } ] const result = wishesData.map((ele) => { return {...ele, score: scoresData.find(s => s.name === ele.name)?.score || 0 } }) console.log("este es el resultado=>", result)

You return the whole object, just return the score:你返回整个 object,只返回分数:

 const wishesData = [{ name: "Peter", presents: ["coffee", "holidays"] }, { name: "Mario", presents: ["coffee", "videogames"] }, { name: "Amanda", presents: ["computer", "tattoo"] }, { name: "Another", presents: ["computer", "tattoo"] } ] const scoresData = [{ name: "Peter", score: 10 }, { name: "Mario", score: 2.3 }, { name: "Amanda", score: 1.1 } ] const result = wishesData.map(ele => { const match = scoresData.find(s => s.name === ele.name) return {...ele, score: match? match.score: 0 } }) console.log("este es el resultado=>", result)

const wishesWithScores = wishesData.map(wishObject => {
  const descriptor = wishObject.name;
  const { score } = scoresData.find(({ name }) => name === descriptor);

  return {
    ...wishObject,
    score
  }
});

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

相关问题 将 object 数组中的数据添加到 JavaScript 中的另一个数组 - add data from array of object to another in JavaScript 如何从一个 object 数组获取数据到另一个 object 数组? Javascript - How to get data from an object array to another object array ? Javascript 如何在javascript中将对象添加到数组中的另一个对象 - How to add an object to another object in array in javascript 如何从 javascript 中的对象数组在另一个 object 数组中创建一个 object 数组 - How to create an array of object inside another array of object from an array of Objects in javascript 如何在JavaScript中将一个数组对象添加到另一个数组对象 - How to add one array object to the another array object in javascript 如何将值从数组中的对象添加到另一个数组中的另一个对象? - How to add value from object in an array into another object in another array? 用JavaScript中另一个数组对象中的数据替换数组中的对象 - Replace object in array with data from another array's object in JavaScript 如何将 object 添加到另一个对象数组中的数组? - How to add object to an array inside another array of objects? 如何在对象内打印Javascript数据数组 - How to print Javascript data Array inside object 如何在javascript中的另一个对象数组中找到数组的长度 - How to find length of an array inside another array of object in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM