【问题标题】:Extracting all values from a Array of Objects based on keys value [duplicate]根据键值从对象数组中提取所有值[重复]
【发布时间】:2019-05-17 18:32:50
【问题描述】:

如果我有一个如下所示的数组:

[{"matchedKey":"cuisineType","cuisineType":"Indian","group":"group"},
 {"matchedKey":"cuisineType","cuisineType":"Italian","group":"group"},
 {"matchedKey":"cuisineType","cuisineType":"Asian","group":"group"},
 {"matchedKey":"cuisineType","cuisineType":"Japanese","group":"group"},
 {"matchedKey":"cuisineType","cuisineType":"African","group":"group"}]

如何按美食类型对其进行排序?

我试过用:

var result = array.find(obj => {
var go =  obj.cuisineType
console.log(String(obj.cuisineType))
})

但我不知道如何:

  1. 把它放在一个字符串中,用命令分隔结果(它们只是单独打印到控制台)。

  2. 使用我刚刚创建的字符串console.log(go)console.log(result) 打印'undefined'。

提前感谢您的帮助!我已经尝试过关于 SO 及其他方面的其他建议,但没有取得太大成功!

【问题讨论】:

    标签: javascript jquery html arrays object


    【解决方案1】:

    最简单的做法是使用map() 构建一个美食数组。然后你可以循环遍历它或根据需要从它构建一个字符串。

    var arr = [{"matchedKey":"cuisineType","cuisineType":"Indian","group":"group"},
      {"matchedKey":"cuisineType","cuisineType":"Italian","group":"group"},
      {"matchedKey":"cuisineType","cuisineType":"Asian","group":"group"},
      {"matchedKey":"cuisineType","cuisineType":"Japanese","group":"group"},
      {"matchedKey":"cuisineType","cuisineType":"African","group":"group"}]
    
    var cuisines = arr.map(function(el) {
      return el.cuisineType;
    });
    
    console.log(cuisines); // array
    console.log(cuisines.join(', ')); // formatted string

    【讨论】:

    • 更短的,把el => el.cuisineType放到你的map函数中
    • @Treycos 确实如此,但要小心箭头函数,因为它们在任何版本的 IE 中都不起作用。
    【解决方案2】:

    只需.map() 进入一个关于美食的新数组。然后对这些数据做任何你想做的事情:

    var arr = [{"matchedKey":"cuisineType","cuisineType":"Indian","group":"group"},
      {"matchedKey":"cuisineType","cuisineType":"Italian","group":"group"},
      {"matchedKey":"cuisineType","cuisineType":"Asian","group":"group"},
      {"matchedKey":"cuisineType","cuisineType":"Japanese","group":"group"},
      {"matchedKey":"cuisineType","cuisineType":"African","group":"group"}];
    
    var sortedArr = arr.map(e => e.cuisineType);
    console.log(sortedArr);

    【讨论】:

      【解决方案3】:

      如果您是 Javascript 新手,但之前使用过任何其他编程语言,这可能是解决当前难题的一种更简单但效率不高的方法。为了便于阅读和理解每一步,我已经用适当的 cmets 编写了 js 的每一步。

      /****** COMMENT *******
      Declaring the original array as arr
      *********************/
      
      var arr = [
              {
               matchedKey:"cuisineType",
               cuisineType:"Indian",
               group:"group"
              },
      
              {
                  matchedKey:"cuisineType",
                  cuisineType:"Italian",
                  group:"group"
              },
      
              {
                  matchedKey:"cuisineType",
                  cuisineType:"Asian",
                  group:"group"
              },
      
              {
                  matchedKey:"cuisineType",
                  cuisineType:"Japanese",
                  group:"group"
              },
      
              {
                  matchedKey:"cuisineType",
                  cuisineType:"African",
                  group:"group"
              }
           ];
      
      /****** COMMENT *******
      Declaring a new empty array
      *******************/
      
      var newArray = [];
      
      /****** COMMENT *******
      array.push() method adds one or more elements to the end of an array.
      So, I'm appending values of key - cuisineType - in the empty array named newArray (see above).
      *******************/
      
      for (var i = 0; i < arr.length; i++){
      
      /****** COMMENT *******
      Through this for loop, 'i' will point as index value to each individual object within an array starting from 0 up to the (length of an array - 1) ==> in your case, 5 - 1 = 4.
      *******************/
      
      newArray.push(arr[i].cuisineType);
      
      }
      
      /****** COMMENT *******
      join() method creates and returns a new string by concatenating all of the elements in an array
      *******************/
      
      console.log(newArray.join(', '));
      

      console.log 中的输出将是

      Indian, Italian, Asian, Japanese, African
      

      希望这是您所寻找的。祝你有美好的一天。

      【讨论】:

        【解决方案4】:

        使用map函数

        const cuisineTypes = [{
            "matchedKey": "cuisineType",
            "cuisineType": "Indian",
            "group": "group"
          },
          {
            "matchedKey": "cuisineType",
            "cuisineType": "Italian",
            "group": "group"
          },
          {
            "matchedKey": "cuisineType",
            "cuisineType": "Asian",
            "group": "group"
          },
          {
            "matchedKey": "cuisineType",
            "cuisineType": "Japanese",
            "group": "group"
          },
          {
            "matchedKey": "cuisineType",
            "cuisineType": "African",
            "group": "group"
          }
        ];
        
        const result = cuisineTypes.map(x => x.cuisineType);
        
        console.log(JSON.stringify(result, null, 4));

        【讨论】:

          【解决方案5】:

          有点 ES6:

            function extractArrayFromKey(ArrayOfObjects,WantedKey){
               return ArrayOfObjects(obj => {
                 let val = null;
                 Object.entries(obj).forEach(([key,value]) => {if (key==WantedKey) val=value});
                 return val
              })
            }
          
           const newArray = extractArrayFromKey(arr,'cuisineType') 
          
           -> ["Indian", "Italian", "Asian", "Japanese", "African"]
          

          【讨论】:

            猜你喜欢
            • 2020-11-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-01-14
            • 2020-09-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多