[英]How to add objects inside array javascript
這是我的數組:
array = [
{ animal: 'dog', leg: 4, age: 3 },
{ animal: 'chicken', leg: 2, age: 2 },
{ animal: 'egg'},
];
這是我想要的數組:
desiredArray = [
{ animal: 'dog', leg: 4, age: 3, sum: 7 },
{ animal: 'chicken', leg: 2, age: 2, sum: 4 },
{ animal: 'egg', sum: 0 },
];
我想為數組中的每個 object 添加一個屬性,即腿和年齡的總和。 如果 leg 或 age 是null
,它的值為 0。
您可以在每個循環中使用 javascript
var array = [
{ animal: 'dog', leg: 4, age: 3 },
{ animal: 'chicken', leg: 2, age: 2 },
{ animal: 'egg'},
];
$.each(array,function(k,v){
var total=0;
if(v.leg)
total=v.leg;
if(v.age)
total=total+v.age
v.sum=total
});
console.log(array);
您可以使用Array.map
function 並像這樣在普通 javascript 本身中直接進行驗證
const array = [ { animal: 'dog', leg: 4, age: 3 }, { animal: 'chicken', leg: 2, age: 2 }, { animal: 'egg'}, ]; const output = array.map(val => ({...val, sum: (val.leg || 0) + (val.age || 0)})); console.log(output);
請嘗試一個它會為你工作,並讓我知道。 我在這里使用array.map
循環對象數組,然后使用結果值向 object 添加一個新屬性。
Object.assign()
方法用於將所有可枚舉自身屬性的值從一個或多個源對象復制到目標 object。 它將返回目標 object。
const result = array.map((el) => {
const o: any = Object.assign({}, el);
o.sum = (el.leg || 0) + (el.age || 0)
return o;
})
array = [ { animal: 'dog', leg: 4, age: 3 }, { animal: 'chicken', leg: 2, age: 2 }, { animal: 'egg'}, ]; resultArray = [] for(var item of array) { item['sum'] = ('leg' in item?item.leg:0) + ('age' in item?item.age:0) resultArray.push(item) } console.log(resultArray)
const array = [ { animal: 'dog', leg: 4, age: 3 }, { animal: 'chicken', leg: 2, age: 2 }, { animal: 'egg'}, { animal: 'special animal', leg: 5}, ]; const getDesiredArray = (array) => { const desiredArray = array.map( animal => { if(animal.leg || animal.age) { animal.sum = animal.leg || 0 + animal.age || 0; } else { animal.sum = 0; } return animal; }) return desiredArray; } console.log(getDesiredArray(array));
如果您有多個數據,那么您可以對其進行解構並使用reduce
方法。 這是一個工作示例:
var array = [ { animal: 'dog', leg: 4, age: 3 }, { animal: 'chicken', leg: 2, age: 2 }, { animal: 'egg'}]; var result = array.map(({animal, ...rest})=>({animal,...rest, sum:Object.values(rest).reduce((s,v)=>s+v,0)})); console.log(result);
因為您在此代碼中使用了對象,所以我認為此代碼的最佳方法是 JavaScript Object 面向方法。 使用這種方法,您的對象本身將具有 sum 屬性。 當您需要查看總和值時,您所要做的就是調用它。
我認為您正在學習 javascript,所以我在這里發布了 ES5 和 ES6 代碼。
//ES5 solution var Animal = function (animalName, leg = null, age = null, sum = null) { this.animalName = animalName; this.leg = leg; this.age = age; this.sum = sum; //you can set this as this.sum = null if you dont want to assign value when define. }; Animal.prototype.calcSum = function () { if (this.age === null || this.leg === null) { // I used null value, because you mentioned it. In javascript undefined variable value is undefined not null. if you mean 0 or undefined? you can change it. this.sum = 0; } else { this.sum = this.leg + this.age; } }; var dog = new Animal("dog", 4, 3); var chicken = new Animal("chicken", 2, 2); var egg = new Animal("egg"); var array = [dog, chicken, egg]; array[0].calcSum(); //this will update the value of sum in dog object array[1].calcSum(); //this will update the value of sum in chicken object array[2].calcSum(); //this will update the value of sum in egg object //ES6 solution class Animal { constructor(animalName, leg = null, age = null, sum = null) { this.animalName = animalName; this.leg = leg; this.age = age; this.sum = sum; } calcSum() { if (this.age === null || this.leg === null) { // I used null value, because you mentioned it. In javascript undefined variable value is undefined not null. if you mean 0 or undefined? you can change it. this.sum = 0; } else { this.sum = this.leg + this.age; } }; } const dog = new Animal("dog", 4, 3); const chicken = new Animal("chicken", 2, 2); const egg = new Animal("egg"); let array = [dog, chicken, egg]; array[0].calcSum(); //this will update the value of sum in dog object array[1].calcSum(); //this will update the value of sum in chicken object array[2].calcSum(); //this will update the value of sum in egg object
您可以使用下面的代碼行獲取總和值。
console.log(array[1].sum);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.