如何动态地将值添加到javascript对象

时间:2022-04-12 07:35:50

I am trying to learn angular, and I have an example code snippet that is the following:

我正在尝试学习角度,我有一个示例代码片段如下:

$scope.contents = [{
    name: 'Chris Doe',
    abbreviation: 'Developer',
},{
    name: 'Ann Doe',
    abbreviation: 'Commerce',
},{
    name: 'Mark Ronson',
    abbreviation: 'Designer',
},{
    name: 'Eric Doe',
    abbreviation: 'Human Resources',
},{
    name: 'John Doe',
    abbreviation: 'Commerce',
},{
    name: 'George Doe',
    abbreviation: 'Media',
},{
    name: 'Ann Ronson',
    abbreviation: 'Commerce',
},{
    name: 'Adam Ronson',
    abbreviation: 'Developer',
},{
    name: 'Hansel Doe',
    abbreviation: 'Social Media',
},{
    name: 'Tony Doe',
    abbreviation: 'CEO',
}];

Which works fine, but I want to fetch data from an API and then load the data into the $scope.contents. I have tried the following:

哪个工作正常,但我想从API获取数据,然后将数据加载到$ scope.contents。我尝试过以下方法:

$http(req).success(function(res) {
    console.log(res);
    for (var i = 0; i < res.length; i++) {
        console.log(res[i]);
        //add data into scope contents here .add(res[i])
    }
}).error(function (err) { console.log(err) });

and this works, I am able to loop through the object of data returned from my API, however I cannot figure out the syntax to add the returned data into $scope.contents What is the proper way to do this? Is there a better way to do this? Like making $scope.contents a class and adding the data as a new instance?

这工作,我能够遍历从我的API返回的数据的对象,但我无法弄清楚将返回的数据添加到$ scope.contents的语法是什么方法这样做的正确方法是什么?有一个更好的方法吗?就像将scope.contents作为一个类并将数据添加为新实例一样?

3 个解决方案

#1


5  

You can use Array#concat:

你可以使用Array#concat:

$scope.contents = $scope.contents.concat(res);

Obviously you first want to make sure that $scope.contents is already an array:

显然,您首先要确保$ scope.contents已经是一个数组:

if (! Array.isArray($scope.contents)) {
  $scope.contents = [];
}
$scope.contents = $scope.contents.concat(res);

#2


2  

$scope.contents is an array, so you can push a new object to it.

$ scope.contents是一个数组,因此您可以将一个新对象推送到它。

$http(req).success(function(res) {
    console.log(res);
    for (var i = 0; i < res.length; i++) {
        console.log(res[i]);
        $scope.contents.push(res[i]);
    }
}).error(function (err) { console.log(err) });

It's not obvious from your example what res[i] contains. The above code is assuming that it is already the correct object. If not, take this and place the correct values.

从你的例子中可以看出res [i]包含的内容并不明显。上面的代码假设它已经是正确的对象。如果没有,请取下并放置正确的值。

$scope.contents.push({
    name: res[i].name,
    abbreviation: res[i].abbreviation,
}); // example -- change to match your data

#3


1  

$scope.contents looks like an array so...

$ scope.contents看起来像一个数组所以......

$scope.contents.push(res[i]);

#1


5  

You can use Array#concat:

你可以使用Array#concat:

$scope.contents = $scope.contents.concat(res);

Obviously you first want to make sure that $scope.contents is already an array:

显然,您首先要确保$ scope.contents已经是一个数组:

if (! Array.isArray($scope.contents)) {
  $scope.contents = [];
}
$scope.contents = $scope.contents.concat(res);

#2


2  

$scope.contents is an array, so you can push a new object to it.

$ scope.contents是一个数组,因此您可以将一个新对象推送到它。

$http(req).success(function(res) {
    console.log(res);
    for (var i = 0; i < res.length; i++) {
        console.log(res[i]);
        $scope.contents.push(res[i]);
    }
}).error(function (err) { console.log(err) });

It's not obvious from your example what res[i] contains. The above code is assuming that it is already the correct object. If not, take this and place the correct values.

从你的例子中可以看出res [i]包含的内容并不明显。上面的代码假设它已经是正确的对象。如果没有,请取下并放置正确的值。

$scope.contents.push({
    name: res[i].name,
    abbreviation: res[i].abbreviation,
}); // example -- change to match your data

#3


1  

$scope.contents looks like an array so...

$ scope.contents看起来像一个数组所以......

$scope.contents.push(res[i]);

相关文章