我有这个对象:
"cuisines":{
"2": "Burgers",
"7": "American",
"9": "Sandwiches",
"11": "Breakfast"
}
我怎样才能把它变成这样?
Burgers, American, Sandwiches, Breakfast
我正在使用角度。
我有这个对象:
"cuisines":{
"2": "Burgers",
"7": "American",
"9": "Sandwiches",
"11": "Breakfast"
}
我怎样才能把它变成这样?
Burgers, American, Sandwiches, Breakfast
我正在使用角度。
As the order of properties in an object is not guaranteed, you would have to sort the properties to get them in that order. Put them in an array and sort them, then you can put the names in an array and join it:
var o = {
"2": "Burgers",
"7": "American",
"9": "Sandwiches",
"11": "Breakfast"
};
var arr = [];
for (key in o) {
arr.push({ key: key, value: o[key] });
}
arr.sort(function(x,y){ return x.key - y.key});
var names = [];
for (var i = 0; i < arr.length; i++) {
names.push(arr[i].value);
}
var result = names.join(', ');
我不知道你为什么想要它,但你可以试试这个,
var cuisine = {
"2": "Burgers",
"7": "American",
"9": "Sandwiches",
"11": "Breakfast"
};
var data =[];
$.each(cuisine, function(key,val){
data.push(val);
});
console.log(data.join(','));
这似乎是一个非常奇怪的问题,并且与 Angular 本身没有任何关系。这似乎是一个纯粹的 javascript 问题。
话虽这么说,你应该能够做到这一点:
var values = [];
for(var key in cuisines){
if(cuisines.hasOwnProperty(key)){
values.push(cuisines[key]);
}
}
// values now looks like ["Burgers","American","Sandwiches","Breakfast"]