简体   繁体   English

向数组中的现有对象添加属性

[英]adding properties to an existing object inside an array

I am picking an array with few object on it from an external file. 我正在从外部文件中选择一个几乎没有对象的数组。

Using: 使用方法:

app.controller('mycontroller',  function($scope, $http) { 
$http.get("file")
.then(function(response) {
    $scope.lols = response.data;     
});});

This will give me back something like: 这会给我一些类似的信息:

 $scope.lols = [
{
    prop1: "h1",
    prop2: "h2",
},
{
    prop1: "g1",
    prop2: "g2",
},}

Now I want to add a prop3 in each of the objects how should I do it ? 现在我想在每个对象中添加一个prop3,该怎么办? If I had the data into my js file I will do it manually but picking the data from an external file... 如果我将数据存入js文件中,则可以手动进行,但要从外部文件中选择数据...

I have tried simply doing: 我试着简单地做:

app.controller('mycontroller',  function($scope, $http) { 
$http.get("file")
.then(function(response) {
    $scope.lols = response.data;
    $scope.lols.push = [
    {prop3: "h3"},
    {prop3: "g3"}
    ]
});});

But it has not worked... 但这没有用...

Thanks for any help or link explain it. 感谢您的帮助或链接进行解释。

SOLUTION: https://jsfiddle.net/d3c96e0z/3/ 解决方案: https : //jsfiddle.net/d3c96e0z/3/

I think this should help you:- 我认为这应该对您有帮助:

var lols = [
{
    prop1: "h1",
    prop2: "h2",
},
{
    prop1: "g1",
    prop2: "g2",
}
]

Object.keys(lols).map(function(key,index){
        var lol = lols[key]
      lol["props3"]="g3"
      console.log(lol)
})

JS fiddle link https://jsfiddle.net/bps7zzf8/ JS小提琴链接https://jsfiddle.net/bps7zzf8/

尝试一下, 这里是工作示例 $scope.lols.push({prop1: "h3",prop2: "g3"});

You want prop3 to be a sibling of prop1 and prop2 ? 你想prop3成为的兄弟prop1prop2 If so you can do it either hard-coded like: 如果是这样,您可以按照以下方式进行硬编码

$scope.lols[0].prop3 = "h3";
$scope.lols[1].prop3 = "g3";

Or in a more dynamic way : 或者以更动态的方式

var newProps = ["h3", "g3"];
for (var i = 0; i < $scope.lols.length; i++) {
  $scope.lols[i].prop3 = newProps[i]; // assuming the length of both arrays is the same size;
}

The problem with your approach: With push you're creating a new index in the array instead of adding your properties to existing indexes. 方法的问题:使用push您正在数组中创建新索引,而不是将属性添加到现有索引中。

if you want to add prop3 in each object can try this process: 如果要在每个对象中添加prop3 ,请尝试以下过程:

// assume that you know "lols" length. here let 2 so add 2 values for prop3

$scope.prop3Values = ["yellow", "red"];

$scope.lols = $scope.lols.map(function(e,index) {
    e.prop3 = $scope.prop3Values[index]; // assign your value in prop3
    return e;
  });

then your lols will be look like 然后你的lols会像

$scope.lols = [
{
    prop1: "h1",
    prop2: "h2",
    prop3: "yellow"
},
{
    prop1: "g1",
    prop2: "g2",
    prop3: "red"
}]

Keep the properties to be added in a array of objects and then loop over the $scope.lols and add each property according to the index; 将要添加的属性保留在对象数组中,然后遍历$ scope.lols并根据索引添加每个属性;

$scope.propertyJson=[{prop3: "h3"},{prop3: "g3"}];
for (var i = 0; i < $scope.lols.length; i++) {
 angular.forEach($scope.propertyJson[i], function(value, key) {
  $scope.lols[i][key] = value;
 });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM