将对象简化为简单数组

时间:2019-06-20 19:08:21

标签: javascript jquery

我有

的对象
{
0: "United States",
1: "India",
2: "Germany",
3: "Brazil",
4: "Taiwan",
5: "Israel",
6: "United Kingdom"
}

我只想使其像仅使用jQueryJavascript

["United States", "India", "Germany".....]

我已经尝试了一些代码,例如

  

.reduce()、. flat()或.concat()

但仍然无法正常工作

4 个答案:

答案 0 :(得分:1)

您可以将对象分配给数组。这种方法尊重键。

var object = { 0: "United States", 1: "India", 2: "Germany", 3: "Brazil", 4: "Taiwan", 5: "Israel", 6: "United Kingdom" },
    array = Object.assign([], object);

console.log(array);

答案 1 :(得分:1)

$.map({0:"united states", 1: "India"}, function(q,w){return(w)})
> ["united states", "India"]

答案 2 :(得分:0)

解决方案1 ​​

var obj= {
  0: "United States",
1: "India",
2: "Germany",
3: "Brazil",
4: "Taiwan",
5: "Israel",
6: "United Kingdom"
}

var result = Object.values(obj)

console.log(result);

解决方案2

var result = Object.keys(obj).map(function(key) {
  return obj[key];
});

console.log(result);

答案 3 :(得分:0)

  const object = {
    0: 'United States',
    1: 'India',
    2: 'Germany',
    3: 'Brazil',
    4: 'Taiwan',
    5: 'Israel',
    6: 'United Kingdom'
  };

  const countries = Object.values(object); 

国家/地区将包含您想要的列表