簡體   English   中英

在數組對象中查找元素

[英]Find Element in Array Object

我想檢查對象數組中是否存在某個值。

例如,如果我有這樣的事情:

[ { _id: 1,    
    name: foo },
  { _id: 2,    
    name: bar },
    { _id: 3,    
    name: foox },
    { _id: 4,    
    name: fooz },
    ]


var search = [1,25,33,4,22,44,5555,63]

然后我想檢查search中的值之一是否在對象數組中包含的對象之一中。

var list = [ { _id: 1, name: "foo" },
  { _id: 2, name: "bar" },
  { _id: 3, name: "foox" },
  { _id: 4, name: "fooz" },
];


var search = [1,25,33,4,22,44,5555,63];

list.forEach(function(element){
    if(search.indexOf(element._id) != -1){
        console.log("found");
    }
});

試試這個,希望這就是你要找的。

使用some迭代對象數組。 如果一個 id 被發現some短路並且不繼續迭代的其余部分。

 const data=[{_id:1,name:'foo'},{_id:2,name:'bar'},{_id:3,name:'foox'},{_id:4,name:'fooz'}]; const search = [1,25,33,4,22,44,5555,63] function finder(searh) { return data.some(obj => { return search.includes(obj._id); }); } console.log(finder(data, search));

如果:

var o = [{ _id: 1, name: "foo"}, { _id: 2, name: "bar"}, { _id: 3, name: "foox"}, { _id: 4, name: "fooz"}];
var search = [1, 25, 33, 4, 22, 44, 5555, 63];

嘗試這個:

var outPus = o.filter(function(u){ 
    return search.some(function(t){ return t == u._id}) 
})

或這個:

var outPut = [];
search.forEach(function(u){ 

    o.forEach(function(t){ 

        if(t._id == u) outPut.push(t) 
    }) 
})
var list = [ { _id: 1,    
    name: foo },
  { _id: 2,    
    name: bar },
    { _id: 3,    
    name: foox },
    { _id: 4,    
    name: fooz },
    ];

var isAnyOfIdsInArrayOfObject = function(arrayOfObjects, ids){
   return arrayOfObjects.some(function(el) { return ids.indexOf(el._id) !== -1; });
}
var list = [ 
  { _id: 1, name: 'foo' },
  { _id: 2, name: 'bar' },
  { _id: 3, name: 'foox' },
  { _id: 4, name: 'fooz' }
];

var search = [1,25,33,4,22,44,5555,63];

此代碼構建了一個列表,其中包含search中的所有元素,這些元素也在您的list

var inArr = search.filter(function(index){
  return list.some(function(el){
    return el._id === index;
  });
});
console.log(inArr); // [1,4];

顯然,您可以切換listsearch ,以便獲得一個數組,其中包含在search中引用的list中的元素:

var elements = list.filter(function(el){
  return search.some(function(index){
    return el._id === index;
  });
});
console.log(elements); // [{ _id: 1, name: 'foo' },{ _id: 4, name: 'fooz' }]

下面是 find 方法的示例。 希望這會幫助你。

   // sample item array
   const items = [
                   { name : 'Bike', price : 100 },
                   { name : 'Car',  price : 3000 }
                 ]
   // find method example
   const foundItem = items.find(( item )) => {
         // you can put your desired condition over here to find an element from 
         // javascript array.
         return item.name == 'Bike'
   }

forEach 循環的一個限制是您不能從(外部)方法返回找到的元素。 相反,您可以使用Array.prototype.find如下:

var elt = list.find(e => search.indexOf(e._id)>=0);
if (!elt)
    console.log("Element not found");
else
    console.log("Found element " + elt.name);

注意:這將需要 IE 中的 Array.prototype.find 的 polyfill。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM