【问题标题】:Find items in the array based on the inner array condition根据内部数组条件查找数组中的项
【发布时间】:2019-01-10 02:19:58
【问题描述】:

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

例子:

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

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

【问题讨论】:

标签: arrays angular typescript javascript-objects


【解决方案1】:

如果需要多个项目,可以通过Array#filter过滤数组,检查每个项目的属性description是否包含单词two通过Array#includes(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);

【讨论】:

  • 他可以使用find 而不是filter,这似乎更合适(相同的代码,不同的输出)
  • 注意:不兼容 IE11
  • find 只返回第一项,可能不止一项
  • @SurenSrapyan 这是一个假设,在这种情况下只有一项
  • @SurenSrapyan 因为如果我假设,我会假设这只是一个示例,并且他实际上想要返回 name 属性。你知道它有多大问题吗?
【解决方案2】:

可能有更有效的方法,但我有些方法可以找到解决方案。遍历数组并匹配数组项。

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】:

    你也可以试试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】:

      这个sn-p可能就是你要找的东西

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

      输出:[1]

      【讨论】:

        【解决方案5】:

        为了举例,这里有一个带有旧 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, [])
        
        );

        【讨论】:

          猜你喜欢
          • 2021-02-14
          • 1970-01-01
          • 2018-04-20
          • 1970-01-01
          • 2021-12-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-29
          相关资源
          最近更新 更多