根据内部数组条件在数组中查找项目

时间:2018-08-02 10:34:33

标签: arrays angular typescript javascript-objects

如何在对象数组内部的数组中查找id

示例:

let arrayobjects = [{ 
    id: 1, 
    name: "oinoin", 
    description: ["one", "two", "three"]
}, { 
    id: 2, 
    name: "zatata", 
    description: ["four", "five", "six"]
}];

如何找到单词“ two”的ID?

5 个答案:

答案 0 :(得分:8)

如果需要多个项目,则可以通过Array#filter过滤数组,并通过Array#includes检查每个项目的属性description是否包含单词two (ES7),然后使用Array#map映射结果以仅获得其中id个项目。

let arrayobjects = [
      { id: 1, name: "oinoin", description: ["one", "two", "three"] }, 
      { id: 2, name: "zatata", description: ["four", "five", "six"] }
];

const ids = arrayobjects.filter(item => item.description.includes('two'))
                        .map(item => item.id);
                        
console.log(ids);

如果只有一项,则可以使用Array#find并进行相同的检查

let arrayobjects = [
      { id: 1, name: "oinoin", description: ["one", "two", "three"] }, 
      { id: 2, name: "zatata", description: ["four", "five", "six"] }
];

const item = arrayobjects.find(item => item.description.includes('two'));
                        
console.log(item.id);

答案 1 :(得分:2)

此代码段可能就是您要查找的

arrayobjects.filter( ele => ele.description.includes("two")).map(ele => ele.id)

输出:[1]

答案 2 :(得分:1)

也许有更有效的方法,但是我有些如何找到解决方案的。遍历数组并匹配数组项。

var arrayobjects = [{ id: 1, name: "oinoin", description: ["one", "two", "three"]}, { id: 2, name: "zatata", description: ["four", "five", "six"]}];
arrayobjects.forEach(item => {
  if(item.description.indexOf('two') != -1 ){
  console.log(item.id)
}
})

答案 3 :(得分:1)

您也可以尝试使用indexOf

arrayobjects.filter(f=>f.description.indexOf('two')>-1)[0].id

let arrayobjects = [
{ 
    id: 1, 
    name: "oinoin", 
    description: ["one", "two", "three"]
}, { 
    id: 2, 
    name: "zatata", 
    description: ["four", "five", "six"]
}
];

console.log(arrayobjects.filter(f=>f.description.indexOf('two')>-1)[0].id);

注意

.filter返回一个数组。因此,如果在说明中找到多个带有two的项目,则不要使用索引[0]。这只是数组中一项的示例。

答案 4 :(得分:0)

举例来说,这是一个带有旧javascript的版本。

  var arrayobjects = [
    { 
        id: 1, 
        name: "oinoin", 
        description: ["one", "two", "three"]
    }, { 
        id: 2, 
        name: "zatata", 
        description: ["four", "five", "six"]
    }
  ];

  function findId (desc, arr) {
    var idx, arrObjLen = arr.length, results = [];

    // Iterate through the first array
    for (idx = 0; idx < arrObjLen; idx ++) {
      // If the value exists in the 'description' array
      if (arr[idx].description.indexOf(desc) > -1) {
        // Add it to the results array
        results.push(arr[idx].id)
      }
    }
    return results;
  }

  console.log('Results:', findId('five', arrayobjects));

如果要最大程度地减少使用的代码量,也可以使用Array#reduce

const arrayobjects = [
    { 
        id: 1, 
        name: "oinoin", 
        description: ["one", "two", "three"]
    }, { 
        id: 2, 
        name: "zatata", 
        description: ["four", "five", "six"]
    }
  ];

console.log(

  arrayobjects.reduce((col, { id, description }) => 
    // If description contains the desired value
    description.includes('two')
      // Add the id to the collector array
      ? [ ...col, id ] : col, [])

);