簡體   English   中英

比較2個對象以在javascript中創建對象的新數組

[英]Compare 2 objects to create a new array of objects in javascript

我有兩個要素:水果和板條箱

fruits是一個包含不同水果列表的數組,例如:

["apple","orange","mango","pear"]

crates是一系列包含水果的對象,例如:

[{id:1,fruit_name: "apple"},{id:2, fruit_name: "pear"}]

我想根據以下條件創建一個新的對象數組:-檢查水果數組中的實體是否存在於任何包裝箱數組中。

如果存在,則最終對象應該具有fruit_name屬性以及另一個稱為tested屬性設置為true 如果不存在,那么tested應該是false ;

考慮以上情況,最終輸出應如下:

[
    {fruit_name: "apple", tested: true},
    {fruit_name: "orange", tested: false},
    {fruit_name: "mango", tested: false},
    {fruit_name: "pear", tested: true},
]

我嘗試過如下:

fruits.forEach(x => {
                        crates.filter((y) => {
                            let temp;
                            if (x == y.fruit_name) {
                                temp = {
                                    fruit: x,
                                    tested: true
                                }
                            }
                            else {
                                temp = {
                                    time: x,
                                    tested: false
                                }
                            }
                            testedCrates.push(temp);
                        })
                    })

這樣做的問題是,它會將每個水果兩次返回,並同時包含兩個用於測試屬性的值。

我得到的輸出如下:

[
    {fruit: "apple", tested: true}, 
    {time: "apple", tested: false},
    {time: "orange", tested: false},
    {time: "orange", tested: false},
    {time: "mango", tested: false},
    {time: "mango", tested: false},
    {time: "pear", tested: false},
    {time: "pear", tested: false}
]

請為此提供一些解決方案。 此外,如果有更好的方法使用es6方法,將很有幫助。 提前致謝。

您可以使用array#maparray#some檢查crates是否存在水果。

 var fruits = ["apple","orange","mango","pear"], crates = [{id:1,fruit_name: "apple"},{id:2, fruit_name: "pear"}]; var result = fruits.map(fruit => { var tested = crates.some(({fruit_name}) => fruit_name === fruit); return {'fruit_name' : fruit, tested}; }); console.log(result); 

創建一存在於板條箱中的水果,然后將水果數組映射到所需的形式:

 const fruits = ["apple","orange","mango","pear"] const crates = [{id:1,fruit_name: "apple"},{id:2, fruit_name: "pear"}]; const cratesSet = new Set(crates.map(({ fruit_name }) => fruit_name)); const result = fruits.map((fruit_name) => ({ fruit_name, tests: cratesSet.has(fruit_name) })); console.log(result); 

您可以在檢查集合的同時為crates Set ,並為新數組迭代fruits

 var fruits = ["apple", "orange", "mango", "pear"], crates = [{ id: 1, fruit_name: "apple" }, { id: 2, fruit_name: "pear" }], cSet = new Set(crates.map(({ fruit_name }) => fruit_name)), result = fruits.map(fruit_name => ({ fruit_name, tested: cSet.has(fruit_name) })); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

fruits.map(fruit => ({
  fruit_name: fruit,
  tested: crates.some(crate => crate.fruit === fruit),
}));
fruits.map(fruit=>({
    fruit_name: fruit,
    tested: crates.some(x=>x.fruit_name === fruit)
}))
fruits.forEach(x => {
    let temp = { fruit: x, tested: false }

    crates.filter(y => {
        if (x === y.fruit_name)
            temp = { fruit: x, tested: true }
})
testedCrates.push(temp);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM