gpt4 book ai didi

javascript - 我可以将一个对象和一个数组放在另一个数组中吗?

转载 作者:行者123 更新时间:2023-11-30 07:59:25 25 4
gpt4 key购买 nike

我有两个对象,我想将它们组合在一个对象中,然后将它们都放在一个数组中。

vm.data = {};
vm.category_name_image = [] ;

getProductFunc=function(){

$http.get(ConfigCnst.apiUrl+'?route=categories').success(function (res) {
//console.log(res);
var sub_category = res;
console.log(sub_category);
var bagmankey = '-JweP7imFLfnh96iwn-c';
//console.log(bagmankey);
angular.forEach(sub_category, function (value, key) {
if (value.parentKey == bagmankey ) {
// where i loop
vm.data.name = value.name;
vm.data.picture = value.image;

var selected = {
vm.data.name,
vm.data.picture
} // Where I group the two.

vm.category_name_image.push(seleted);
// where i want to place the both.


}

});


});
}

当我将 vm.data.name 和 vm.data.picture 都放在所选对象中时,我似乎遇到了错误。

我想要这样的输出:[ {name,picture},{name,picture},{name,picture} ]

最佳答案

你不能创建没有名字的对象属性:

//object with properties 'name' & 'picture'
var selected = {
name: vm.data.name,
picture: vm.data.picture
}

或者你可以使用数组,如果你真的只需要使用数据(糟糕的方式):

var selected = [
vm.data.name,
vm.data.picture
]

关于javascript - 我可以将一个对象和一个数组放在另一个数组中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32041957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com