我有兩個物件陣列arr 1和arr 2
arr1 = [{fruit: 'apple',count: 0}] arr2 = [{fruit: 'apple',count: 0}]
需要創建一個接受arr1和arr2作為引數的函式,并比較水果名稱,如果其相同,則需要根據輸入的計數更新計數值,否則將該值推到陣列中
示例輸出 1:輸入。
arr1 = [{fruit: 'apple',count: 10}] arr2 = [{fruit: 'apple',count: 30},{fruit: 'orange',count: 20}]
輸出。
[{fruit: 'apple',count: 40},{fruit: 'orange',count: 20}]/code>
示例輸出2
Inputs:
arr1 = [{fruit: 'apple',count: 15}] arr2 = [{fruit: 'orange',count: 20}]
輸出: [{fruit: 'orange',count: 20}]。
輸出。
[{fruit: 'apple',count: 15},{fruit: 'orange',count: 20}]
我試過的代碼:
function compareAndUpdateCount(arr1, arr2) {
for (i = 0; i < arr1。 length; i ) {
if (arr1[i].fruit === arr2[i].fruit) {
arr1[i].count = arr1[i].count arr2[i] ;
} else {
arr1.push(arr2[i] )
}
}
當陣列長度相同時,這個方法很好用,如果它不同,需要幫助撰寫優化代碼,如果我們有更多的資料,也需要降低時間的復雜性
。uj5u.com熱心網友回復:
你將需要在你的類中為count設定一個設定器 做一個新的物件陣列temparr
temparr[0] = arr1[0] 。
if((arr1[0].fruit).equals(arr2[0].fruit) ) {
temparr[0].setcount(arr1[0].count arr2[0].count) 。
}
else {
temparr[1]=arr2[0] 。
}
在結尾處u列印temparr
。uj5u.com熱心網友回復:
。function groupObject(arr1, arr2) {
const map = new Map() 。
const temp = [...arr1, ...arr2].reduce((acc, { fruit, count }) => /span> {
map.has(fruit)
? (map.get(fruit).count = map.get(fruit).count count)
: map.set(fruit, { fruit, count })。
return acc;
}, map)。
return [...map.values()]。
}
const arr1 = [{ fruit: "apple"/span>, count: 15 }]。
const arr2 = [{ fruit: "orange", count: 20 }]。
console.log(groupObject(arr1, arr2))。
const arr3 = [{ fruit: "apple"/span>, count: 0 }]。
const arr4 = [{ fruit: "apple"/span>, count: 0 }]。
console.log(groupObject(arr3, arr4));
/* This is not a part of answer. 它只是為了給輸出填充高度。所以請忽略它 */
.as-console-wrapper { max-height: 100% ! important; top: 0; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
var result;
if(arr1.length < 1) {
結果 = arr2;
} else if(arr2.length < 1) {
結果 = arr1;
} else {
var existing = [] 。
var arr3 = [];
arr1.forEach(function (item, index) {
arr2.forEach(function (item2, index2) {
if(item.fruit == item2.fruit) {
item.count = item.count item2.count。
existing.push(index2);
} else {
arr3.push(item)。
if(!existing.includes(index2)) {
arr3.push(item2)。
}
}
});
});
結果 = arr3;
}
uj5u.com熱心網友回復:
在另一個回圈中回圈,比較所有的物件屬性,然后使用if陳述句對計數器進行求和,并使用splice()
試一下下面的代碼段:
。var arr1 = [{fruit: 'apple',count: 10}]。
var arr2 = [{fruit: 'apple',count: 30}, {fruit: 'orange',count: 20}, {fruit: 'apple',count: 20}, {fruit: 'banana',count: 20}]。
var counter = 0;
for(obj1 in arr1){
for(obj2 in arr2){
if (arr1[obj1].fruit ==arr2[obj2].fruit) {
arr1[obj1].count = arr1[obj1].count arr2[obj2].count;
arr2.splice(counter, 1)。
counter ;
}
}
}
arr1 = arr1.concat(arr2)。
console.log(arr1)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
嘗試使用reduce將水果和它的數量分組,然后用一個簡單的map來獲得所需的陣列
const arr1 = [{fruit: 'apple',count: 10}] 。, arr2 = [{fruit: 'apple',count: 30},{fruit: 'orange',count: 20}]
const res = [...arr1, ...arr2].reduce((acc, curr) => /span> {
acc[corr.fruit] ?
? (acc[corr.fruit].count = curr.count)
: (acc[curr.fruit] = curr)。
return acc;
}, {});
console.log(Object.keys(res)。 map((item) => res[item]));
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/321654.html
標籤:
上一篇:匯編中的主題依賴資源
下一篇:如何只映射影像的部分源屬性
