[英]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.