【问题标题】:Getting/Filtering Object based on value [duplicate]基于值获取/过滤对象[重复]
【发布时间】:2016-11-25 15:48:40
【问题描述】:

我可以根据值过滤或提取对象的值吗?

例如 [10, 19] 将返回 Bill 和 Sam。

 [ { "id": 10, "nice_name": "Bill" }, 
    { "id": 12, "nice_name": "Dan"}, 
    { "id": 18, "nice_name": "Tony" },
    { "id": 19, "nice_name": "Sam" }, 
]

谢谢/

【问题讨论】:

    标签: javascript arrays object filter


    【解决方案1】:

    您可以链接过滤器然后映射函数:

    const mySearch = [10, 19]
    const result = myArray.filter(elem => mySearch.indexOf(elem.id) > -1) // filter by id
                          .map(elem => elem.nice_name) // return the nice_name only for each entry
    // result is now ['Bill', 'Sam']
    

    【讨论】:

      【解决方案2】:

      你可以使用Array.prototype.filter()函数:

      const data = [ { "id": 10, "nice_name": "Bill" }, 
          { "id": 12, "nice_name": "Dan"}, 
          { "id": 18, "nice_name": "Tony" },
          { "id": 19, "nice_name": "Sam" }, 
      ]
      
      const result = data.filter(o => ~[10, 19].indexOf(o.id))
      // ~[10, 19].indexOf(o.id) is equivalent to [10, 19].indexOf(o.id) > -1
      

      【讨论】:

        【解决方案3】:

        只使用 javascript 你可以做这样的事情。

        var size = a.length; //a -> your array
        var inputSize = input.length; // -> the search array
        var thePeople = []; //where you will store the names that match
        for(var i = 0; i < size; i++) {  //cycle your array
            for(var j = 0; j < inputSize; j++) { //cycle the search array
            if(a[i].id === input[j]) { //check if there is a match on id
                thePeople.push(a[i].nice_name); //save the name into a new array
            }
          }
        }
        

        这是一个小提琴https://jsfiddle.net/xo5vxwo0/

        【讨论】:

          【解决方案4】:

          indexOf 在数组中查找元素。

          arr=[ { "id": 10, "nice_name": "Bill" }, 
              { "id": 12, "nice_name": "Dan"}, 
              { "id": 18, "nice_name": "Tony" },
              { "id": 19, "nice_name": "Sam" }, 
          ]
          var fill=[10,19];
          var ans=[];
          
          arr.map(function(a){
              if(fill.indexOf(a["id"])>-1)
              ans.push(a["nice_name"]);
          })
          
          console.log(ans);
          *{
          background-color:pink;
          }

          【讨论】:

            【解决方案5】:
            var data = [ { "id": 10, "nice_name": "Bill" }, 
                     { "id": 12, "nice_name": "Dan"}, 
                     { "id": 18, "nice_name": "Tony" },
                     { "id": 19, "nice_name": "Sam" }, 
            ]
            
            var idWant  = [10,19];
            var content = '';   
            
            for(var keysWant in idWant){
                var number = idWant[keysWant];  
                for(var keysData in data){   
                    if(number == data[keysData]['id']){   
                        content += data[keysData]['nice_name'] + ' ';
                    } 
                }  
            }  
            
            console.log(content);
            

            【讨论】:

            • 只有代码答案是可以的,但如果记录在案会更好。
            【解决方案6】:

            可能会使用单个 reduce 而不是 filter.map 链。

            var arr = [ { "id": 10, "nice_name": "Bill" }, 
                        { "id": 12, "nice_name": "Dan"}, 
                        { "id": 18, "nice_name": "Tony" },
                        { "id": 19, "nice_name": "Sam" }, 
                      ],
                src = [10,19],
             result = arr.reduce((r,o) => src.includes(o.id ) ? r.concat(o.nice_name) : r,[]);
            console.log(result);

            【讨论】:

              猜你喜欢
              • 2019-04-29
              • 1970-01-01
              • 2020-07-04
              • 2019-09-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-12-11
              • 2017-06-05
              相关资源
              最近更新 更多