从与特定参数匹配的二维数组中检索对象[重复]

时间:2022-02-26 13:17:57

This question already has an answer here:

这个问题在这里已有答案:

So I have a 2d array containing multiple objects. Each object has multiple properties and methods. I would like to return only the objects methods and properties that have a matching id to what I pass it. in this case, the id is 1.

所以我有一个包含多个对象的二维数组。每个对象都有多个属性和方法。我想只返回具有匹配id的对象方法和属性到我传递它的内容。在这种情况下,id为1。

const blogData = [
    {
        title : "Title 1",
        date : "2017-07-15",
        id : 1
    },
    {
        title : "Title 2",
        data : "2017-07-16",
        id : 2
    }
];

for (let i = 0; i < blogData.length; i++) {
  if (blogData[i].id === 1) {
      console.log(`Post #${blogData[i].id} loaded`);
  }                        
}

3 个解决方案

#1


2  

You can filter the array based on ID, and assuming you just have one hit, you can return the first (and only) item, or skip shift() and get an array of matches.

您可以根据ID过滤数组,并假设您只有一个匹配,您可以返回第一个(也是唯一的)项,或跳过shift()并获得一系列匹配项。

const blogData = [{
    title: "Title 1",
    date: "2017-07-15",
    id: 1
  },
  {
    title: "Title 2",
    data: "2017-07-16",
    id: 2
  }
];

var result = blogData.filter( x => x.id === 1).shift();

console.log(result)

#2


0  

You can use a generic function that will work with any ID and any list.
Something like:

您可以使用适用于任何ID和任何列表的通用函数。就像是:

    const blogData = [
        {
            title : "Title 1",
            date : "2017-07-15",
            id : 1
        },
        {
            title : "Title 2",
            data : "2017-07-16",
            id : 2
        }
    ];
    
    function getData(id, arr, callback){
       $.each(arr, function(key, value){
       		if(value.id === id)
          	callback(value); //Just using a simple callback for console purposes
          });
    }
    
    $(document).ready(function(){
    	getData(1, blogData, function(c){
        console.log(c); //loggin the callback
      });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

#3


0  

In your case this is an array of objects and not a 2 dimentional array, so you can use Array.prototype.filter() method to filter only the object that has the id=1:

在您的情况下,这是一个对象数组而不是2维数组,因此您可以使用Array.prototype.filter()方法仅过滤具有id = 1的对象:

var res = blogData.filter(function(obj){
  return obj.id === searchedId;
}).shift();

Demo:

const blogData = [
    {
        title : "Title 1",
        date : "2017-07-15",
        id : 1
    },
    {
        title : "Title 2",
        data : "2017-07-16",
        id : 2
    }
];

var searchedId = 1;

var res = blogData.filter(function(obj){
  return obj.id === searchedId;
}).shift();
console.log(res);

And using your method with foor loop, you just need to return the right object if the condition is matched:

并且使用带有foor循环的方法,如果条件匹配,则只需返回正确的对象:

var result = {};
for (let i = 0; i < blogData.length; i++) {
  if (blogData[i].id === 1) {
      result = blogData[i]
  }                        
}

#1


2  

You can filter the array based on ID, and assuming you just have one hit, you can return the first (and only) item, or skip shift() and get an array of matches.

您可以根据ID过滤数组,并假设您只有一个匹配,您可以返回第一个(也是唯一的)项,或跳过shift()并获得一系列匹配项。

const blogData = [{
    title: "Title 1",
    date: "2017-07-15",
    id: 1
  },
  {
    title: "Title 2",
    data: "2017-07-16",
    id: 2
  }
];

var result = blogData.filter( x => x.id === 1).shift();

console.log(result)

#2


0  

You can use a generic function that will work with any ID and any list.
Something like:

您可以使用适用于任何ID和任何列表的通用函数。就像是:

    const blogData = [
        {
            title : "Title 1",
            date : "2017-07-15",
            id : 1
        },
        {
            title : "Title 2",
            data : "2017-07-16",
            id : 2
        }
    ];
    
    function getData(id, arr, callback){
       $.each(arr, function(key, value){
       		if(value.id === id)
          	callback(value); //Just using a simple callback for console purposes
          });
    }
    
    $(document).ready(function(){
    	getData(1, blogData, function(c){
        console.log(c); //loggin the callback
      });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

#3


0  

In your case this is an array of objects and not a 2 dimentional array, so you can use Array.prototype.filter() method to filter only the object that has the id=1:

在您的情况下,这是一个对象数组而不是2维数组,因此您可以使用Array.prototype.filter()方法仅过滤具有id = 1的对象:

var res = blogData.filter(function(obj){
  return obj.id === searchedId;
}).shift();

Demo:

const blogData = [
    {
        title : "Title 1",
        date : "2017-07-15",
        id : 1
    },
    {
        title : "Title 2",
        data : "2017-07-16",
        id : 2
    }
];

var searchedId = 1;

var res = blogData.filter(function(obj){
  return obj.id === searchedId;
}).shift();
console.log(res);

And using your method with foor loop, you just need to return the right object if the condition is matched:

并且使用带有foor循环的方法,如果条件匹配,则只需返回正确的对象:

var result = {};
for (let i = 0; i < blogData.length; i++) {
  if (blogData[i].id === 1) {
      result = blogData[i]
  }                        
}