2017-01-02 60 views
0

我有對象的數組這就好比推新元件在多維數組obj的陣列的javascript

[ 
 
    { 
 
     "id" : 1, 
 
     "title" : "name1", 
 
     "description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, nesciunt hic aut? Saepe nihil autem nesciunt, ab quisquam animi, aperiam fugit? Ut velit a, in perspiciatis error inventore. Dolorum, eligendi.", 
 
     "image" : "sampledata/products/1.jpg", 
 
     "price" : 10 
 
    }, 
 
    { 
 
     "id" : 2, 
 
     "title" : "name2", 
 
     "description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, nesciunt hic aut? Saepe nihil autem nesciunt, ab quisquam animi, aperiam fugit? Ut velit a, in perspiciatis error inventore. Dolorum, eligendi.", 
 
     "image" : "sampledata/products/2.jpg", 
 
     "price" : 100 
 
    }, 
 
    { 
 
     "id" : 3, 
 
     "title" : "name3", 
 
     "description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, nesciunt hic aut? Saepe nihil autem nesciunt, ab quisquam animi, aperiam fugit? Ut velit a, in perspiciatis error inventore. Dolorum, eligendi.", 
 
     "image" : "sampledata/products/3.jpg", 
 
     "price" : 200 
 
    } 
 
    ]

和我要添加新元素的每個OBJ這樣的,「計數「:12,我試圖使用.push來做到這一點,但它會將數添加到數組的末尾而不是將其添加到每個obj,我該如何解決這個問題?

+0

您可以添加你的JavaScript代碼,或者與你的代碼Plunkr因爲它是目前? –

+1

順便說一下,它不是一個真正的多維數組(它將是[[],[],[]]),它的一個對象數組 –

回答

3
for(elem of array){ 
    elem["count"]=12; 
} 
//OR 
for(key in array){ 
array[key]["count"]=12; 
} 
//OR 
array.forEach(function(elem){ 
    elem["count"]=12; 
} 
//OR 
newarray=array.map(function(el){ 
return el["count"]=12,el; 
} 

您不能添加到多個elems的,你必須循環槽,並添加到每一個。 這取決於你的方式你去(上面的例子)。

此外,還推動數組不是對象(推只增加一個值而不是一個鍵,一個對象需要一個鍵值對)。

0

您可以使用forEach方法添加對象的屬性。

and push不是添加對象屬性的方法。

var arr=[ 
 
    { 
 
     "id" : 1, 
 
     "title" : "name1", 
 
     "description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, nesciunt hic aut? Saepe nihil autem nesciunt, ab quisquam animi, aperiam fugit? Ut velit a, in perspiciatis error inventore. Dolorum, eligendi.", 
 
     "image" : "sampledata/products/1.jpg", 
 
     "price" : 10 
 
    }, 
 
    { 
 
     "id" : 2, 
 
     "title" : "name2", 
 
     "description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, nesciunt hic aut? Saepe nihil autem nesciunt, ab quisquam animi, aperiam fugit? Ut velit a, in perspiciatis error inventore. Dolorum, eligendi.", 
 
     "image" : "sampledata/products/2.jpg", 
 
     "price" : 100 
 
    }, 
 
    { 
 
     "id" : 3, 
 
     "title" : "name3", 
 
     "description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, nesciunt hic aut? Saepe nihil autem nesciunt, ab quisquam animi, aperiam fugit? Ut velit a, in perspiciatis error inventore. Dolorum, eligendi.", 
 
     "image" : "sampledata/products/3.jpg", 
 
     "price" : 200 
 
    } 
 
    ] 
 
arr.forEach(function(a){a["count"] = 12}) 
 
console.log(arr);

0

你可以循環使用Array#forEach並用property accessor

var data = [{ id: 1, title: "name1", description: "Lorem ipsum ...", image: "sampledata/products/1.jpg", price: 10 }, { id: 2, title: "name2", description: "Lorem ipsum ...", image: "sampledata/products/2.jpg", price: 100 }, { id: 3, title: "name3", description: "Lorem ipsum ...", image: "sampledata/products/3.jpg", price: 200 }]; 
 

 
data.forEach(function (a) { 
 
    a.count = 12; 
 
}); 
 

 
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

1

添加一個新的屬性,你會很容易使用的地圖。試試這個:

var result = [ 
    { 
     "id" : 1, 
     "title" : "name1", 
     "description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, nesciunt hic aut? Saepe nihil autem nesciunt, ab quisquam animi, aperiam fugit? Ut velit a, in perspiciatis error inventore. Dolorum, eligendi.", 
     "image" : "sampledata/products/1.jpg", 
     "price" : 10 
    }, 
    { 
     "id" : 2, 
     "title" : "name2", 
     "description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, nesciunt hic aut? Saepe nihil autem nesciunt, ab quisquam animi, aperiam fugit? Ut velit a, in perspiciatis error inventore. Dolorum, eligendi.", 
     "image" : "sampledata/products/2.jpg", 
     "price" : 100 
    }, 
    { 
     "id" : 3, 
     "title" : "name3", 
     "description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, nesciunt hic aut? Saepe nihil autem nesciunt, ab quisquam animi, aperiam fugit? Ut velit a, in perspiciatis error inventore. Dolorum, eligendi.", 
     "image" : "sampledata/products/3.jpg", 
     "price" : 200 
    } 
    ].map(function(item) { 
    item["test"] = "new item"; 
     return item; 
}); 

console.log(result); 
0

如下..

var arr=[ 
    { 
     "id" : 1, 
     "title" : "name1", 
     "description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, nesciunt hic aut? Saepe nihil autem nesciunt, ab quisquam animi, aperiam fugit? Ut velit a, in perspiciatis error inventore. Dolorum, eligendi.", 
     "image" : "sampledata/products/1.jpg", 
     "price" : 10 
    }, 
    { 
     "id" : 2, 
     "title" : "name2", 
     "description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, nesciunt hic aut? Saepe nihil autem nesciunt, ab quisquam animi, aperiam fugit? Ut velit a, in perspiciatis error inventore. Dolorum, eligendi.", 
     "image" : "sampledata/products/2.jpg", 
     "price" : 100 
    }, 
    { 
     "id" : 3, 
     "title" : "name3", 
     "description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, nesciunt hic aut? Saepe nihil autem nesciunt, ab quisquam animi, aperiam fugit? Ut velit a, in perspiciatis error inventore. Dolorum, eligendi.", 
     "image" : "sampledata/products/3.jpg", 
     "price" : 200 
    } 
    ]; 
arr.forEach(function (obj) { 
    obj.count = 12; 
}); 

alert(arr[0].count); 
alert(arr[1].count); 
alert(arr[2].count); 
0

最簡單的方法是你需要使用一個循環

改編爲您的陣列。

for(var i=0;i<arr.length;i++) { 
     var obj = arr[i]; 
    obj.count = 12; 
} 
+0

當時我正在寫我的答案沒有。 –

+0

但你不是最後一個...... –

0

let arr = [ 
 
    { 
 
     "id" : 1, 
 
     "title" : "name1", 
 
     "description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, nesciunt hic aut? Saepe nihil autem nesciunt, ab quisquam animi, aperiam fugit? Ut velit a, in perspiciatis error inventore. Dolorum, eligendi.", 
 
     "image" : "sampledata/products/1.jpg", 
 
     "price" : 10 
 
    }, 
 
    { 
 
     "id" : 2, 
 
     "title" : "name2", 
 
     "description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, nesciunt hic aut? Saepe nihil autem nesciunt, ab quisquam animi, aperiam fugit? Ut velit a, in perspiciatis error inventore. Dolorum, eligendi.", 
 
     "image" : "sampledata/products/2.jpg", 
 
     "price" : 100 
 
    }, 
 
    { 
 
     "id" : 3, 
 
     "title" : "name3", 
 
     "description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, nesciunt hic aut? Saepe nihil autem nesciunt, ab quisquam animi, aperiam fugit? Ut velit a, in perspiciatis error inventore. Dolorum, eligendi.", 
 
     "image" : "sampledata/products/3.jpg", 
 
     "price" : 200 
 
    } 
 
    ] 
 
arr.forEach((val) =>{val['count'] = 12;}) 
 
console.log(arr);

希望這有助於....

+0

真的嗎? 7ths Answer ... –