简体   繁体   English

如何根据每个 object 中的数组从 object 数组中返回某些对象?

[英]How can I return certain objects from object array based on an array inside each object?

For example if I wanted to get a new array of all objects that had a skill of "reading" from the below array, how would I do this?例如,如果我想从下面的数组中获取具有“读取”技能的所有对象的新数组,我该怎么做?

I tried testResult but just get infinite loop:(我尝试了 testResult 但只是得到了无限循环:(

Thanks for all and any help!感谢所有和任何帮助!

const people = [{
    name: "Jon",
    skills: ["reading", "acting", "drinking"],
},
{
    name: "Tim",
    skills: ["rowing", "hockey", "reading"],
},
{
    name: "Lucy",
    skills: ["business", "learning", "hockey"],
},
{
    name: "Michelle",
    skills: ["running", "business", "sleeping"],
},
{
    name: "Michael",
    skills: ["making friends", "surfing"],
}

] ]

Expected return:预期收益:

[{
    name: "Jon",
    skills: ["reading", "acting", "drinking"],
},
{
    name: "Tim",
    skills: ["rowing", "hockey", "reading"],
}]

const testResult = testArray.map((obj) => {
obj.skills.map((skill) => {
    if (skill === "reading") {
        setPeople([...people, obj])
    }
})

}) })

try this尝试这个

people.filter((e) => e.skills.indexOf('reading') > -1)
var people_new = []; 

for(var i = 0; i < people.length; i++) 
{

   var skills = people[i].skills; 
   
   for(var s = 0; s < skills.length; s++) 
   {
      if( skills[s] == "reading" ) 
      {
         people_new.push(people[i]); 
      }
   }

}
let filterd = [] for(item of people){ if(item.skills.includes('reading')){ filterd.push(item) } } console.log(filtered) // array of objs

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

相关问题 根据对象数组内的对象条件返回布尔值 - return boolean based on object condition inside array of objects 如何根据对象的数组克隆 JavaScript 中的对象 - How can I clone objects in JavaScript based on object's array 如何对未排序的对象数组的 OBJECT 中的元素重新排序? - How can I reorder an ELEMENT inside of an OBJECT of an array of OBJECTS NOT sort? 如何在 Javascript 的数组中返回仅包含某个 object 的对象数组? - How do I return an array of objects that only contains a certain object in an array for Javascript? 基于对象内部数组的新对象数组? - New array of objects based on an array inside an object? 如何从多维 object 返回对象数组? - How to return array of objects from multidimensional object? 我想 JSON 解析对象内的对象数组 - 正文中的文章,我该怎么做? - I want to JSON Parse an array of objects inside an object - articles from the body, how can I do that? 从对象数组返回 object - Return object from array of objects 如何仅从对象数组的每个对象返回对象值? - How does one return the object values only from each object of an array of objects? 如何根据javascript中的最大值从对象数组中删除对象 - How can I remove an object from array of objects based on max value in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM