根据键值

时间:2018-12-17 11:34:50

标签: javascript jquery html arrays object

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

[{"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"}]

如何按CuisineType进行排序?

我尝试使用:

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

但是我不知道如何:

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

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

谢谢您的帮助!我曾尝试过其他有关SO的建议,但并没有取得太大的成功!

5 个答案:

答案 0 :(得分:3)

最简单的方法是使用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

答案 1 :(得分:1)

使用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));

答案 2 :(得分: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 sortedArr = arr.map(e => e.cuisineType);
console.log(sortedArr);

答案 3 :(得分:1)

如果您是Java的新手,但以前使用过任何其他编程语言,则这可能是解决当前难题的一种更简单但效率不高的方法。我已经用适当的注释写了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 :(得分:0)

有点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"]