简体   繁体   English

如何在另一个对象数组中获取按值过滤的新数组? javascript

[英]How can I get a new array filtered by value in another array of objects? javascript

I want to get only the array of presents modify by the scoreChild, but result1 include the name also, I am finding little bit hard to understand iteration object inside array with array我只想得到由 scoreChild 修改的礼物数组,但 result1 也包含名称,我发现有点难以理解数组内部的迭代 object

  const wishesData = [
    {
       name: "Peter",
      presents: ["coffee", "holidays"]
    },
    {
       name: "Mario",
      presents: ["coffee", "videogames"]
    },
    {
       name: "Amanda",
      presents: ["computer", "tattoo"]
    },
    {
      name: "David",
      presents: ["car","clothes"]
    }
  ]
  const scoresData= [
    {
      name: "Peter",
      score: 10
    },
    {
      name: "Mario",
      score: 6.3
    },
    {
       name: "Amanda",
      score: 1.1
    },
     {
       name: "David",
      score: 8
    }
  ]

  const childScore = scoresData.find(s=>s.name=== "Amanda").score

  console.log("Amanda score",childScore)

const result1 = wishesData.filter((ele)=>{
 
console.log("estos childScore=>",childScore)
  if(ele.name === "Amanda"){
    if(childScore>7){
    return ele.presents
  } else if(childScore<7 || childScore>5) {
    return  [...ele.presents, "coal"]
  } else if (childScore<5){
    return ele.presents.slice(0,1).concat("coal")
  }
  }

})

console.log(result1)

this is the result I am expected: result1 = ["car","coal"], I mean each time I change the name of child I will get the array with the presents he "deserve" according to his score这是我期望的结果:result1 = ["car","coal"],我的意思是每次我更改孩子的名字时,我都会根据他的分数得到他“应得”的礼物的数组

If I understand this correctly, you want to have different behaviour depending on score.如果我理解正确,您希望根据分数有不同的行为。

For this example, you will never reach the last test in this function对于此示例,您将永远无法到达此 function 中的最后一个测试

  (ele)=>{
 
  console.log("estos childScore=>",childScore)
  if(ele.name === "Amanda"){
    if(childScore>7){
    return ele.presents
  } else if(childScore<7 || childScore>5) {
    return  [...ele.presents, "coal"]
  } else if (childScore<5){
    return ele.presents.slice(0,1).concat("coal")
  }
  }

}

because the part因为这部分

else if(childScore<7 || childScore>5) {
        return  [...ele.presents, "coal"]
      }

will return true for everything, maybe you wanted && instead of ||一切都会返回 true,也许你想要&&而不是||

Another thing, if you want to just have the presents value, filter is not what you need, since it only wants a boolean value in return to create a new array with only values matching the predicate.另一件事,如果你只想拥有礼物值, filter不是你需要的,因为它只需要一个 boolean 值来创建一个新数组,其中只包含与谓词匹配的值。

you can use reduce method here like this你可以像这样在这里使用reduce方法

const result1 = wishesData.reduce((acc, ele)=>{
  console.log("estos childScore=>",childScore)
  if(ele.name === "Amanda"){
    if(childScore>7){
      return ele.presents
    } else if(childScore<7 || childScore>5) {
      return  [...ele.presents, "coal"]
    } else if (childScore<5){
      return ele.presents.slice(0,1).concat("coal")
    }
  }
 return acc;
})

Hope this answers your question: :)希望这能回答你的问题::)

Edit: The part inside can be simplified to编辑:里面的部分可以简化为

if(childScore>7) return ele.presents;
if (childScore<5) return ele.presents.slice(0,1).concat("coal");
return [...ele.presents, "coal"];

or even甚至

return childScore>7 ? ele.presents : [...(childScore < 5 ? ele.presents.slice(0,1) : ele.presents), "coal"];

if you wanted a one-liner (note that [ele.presents[0]] may be more readable than ele.presents.slice(0,1) as well)如果你想要一个单线(注意[ele.presents[0]]也可能比ele.presents.slice(0,1)更具可读性)

暂无
暂无

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

相关问题 如何从 JavaScript 中的对象数组中获取唯一值,但按特定值过滤 - How to get unique values from an array of objects in JavaScript, but filtered by certain value 我可以在不引用 javascript 的情况下从对象数组中的另一个值分配一个对象数组中的项目的值吗? - Can I assign a value, of an item in an array of objects, from another value from an array of objects without referencing in javascript? 如何过滤对象数组并获取过滤后的对象值 - How to filter array of objects and get filtered object value 在 JS 中为过滤的对象数组获取最大值 - Get max value in JS for filtered array of objects 从过滤的对象数组中获取价值 - get value from a filtered array of objects 如何过滤 object 数组,其中包含逐项数组? javascript - How can I filtered array of object with array by item inside it ? javascript 如何通过提取 Javascript/Typescript 中另一个对象数组中的数组来创建对象数组? - How can I create an array of objects by extracting an array that is inside another array of objects in Javascript/Typescript? 如何使用另一个对象中的值过滤对象数组? - How can I filter an array of objects with a value from another object? AngularJS - 如何在另一个过滤器中使用过滤后的数组? - AngularJS - How can i use a filtered array in another filter? 如何从基于本地存储值筛选的 Javascript 数组生成列表? - How can I generate a list from a Javascript Array filtered based on local storage value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM