第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

比較兩個(gè)對(duì)象并獲取共同值 JavaScript

比較兩個(gè)對(duì)象并獲取共同值 JavaScript

HUH函數(shù) 2023-10-20 15:31:22
我想比較兩個(gè)對(duì)象,并想要?jiǎng)?chuàng)建一個(gè)具有共同屬性的新對(duì)象。我見過很多采取差異的解決方案,但不確定我們?nèi)绾尾扇」餐膬r(jià)值觀。這是我的對(duì)象,其中如果 obj1 具有 iscommisonAccount false 并且 obj2 具有 iscommisonAccount true 那么我的結(jié)果不應(yīng)該具有值,因?yàn)槿绻麅烧呔哂邢嗤膶傩?,則它不匹配,那么只能得到結(jié)果。我知道如果我們有對(duì)象數(shù)組,我們可以使用過濾器,但如果我們只有對(duì)象怎么辦?obj 1 = {   "val1":"test",   "stream":{      "iscommisonAccount":false,      "istradeAccount":true   }} obj 2 = {   "val1":"test",   "stream":{      "iscommisonAccount":true,      "istradeAccount":true   }}result = {   "stream":{      "istradeAccount":true   }}diffrent examples:obj 1 = {   "val1":"test",   "stream":{      "iscommisonAccount":false,      "istradeAccount":true   }} obj 2 = {   "val1":"test",   "stream":{      "iscommisonAccount":true,      "istradeAccount":false   }}result = {   "stream":{   }}orobj 1 = {   "val1":"test",   "stream":{      "iscommisonAccount":true,      "istradeAccount":true   }} obj 2 = {   "val1":"test",   "stream":{      "iscommisonAccount":true,      "istradeAccount":true   }}result = {   "stream":{      "iscommisonAccount":true,      "istradeAccount":true   }}
查看完整描述

2 回答

?
慕仙森

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊

像這樣的東西應(yīng)該有效。一個(gè)遞歸函數(shù),僅遍歷對(duì)象的所有鍵。


它不處理數(shù)組,但如果需要的話可以對(duì)其進(jìn)行修改。


function findCommonValues(obj1, obj2) {

    var result = {}

    for (let key in obj1) {

        if (obj1[key] && obj1[key] === obj2[key]) result[key] = obj1[key]

        else if (typeof obj1[key] === 'object' && obj1[key] !== null) {

            result[key] = findCommonValues(obj1[key], obj2[key])

        }

    }

    return result;

}


const obj1 = {

    "val1": "test",

    "stream": {

        "iscommisonAccount": false,

        "istradeAccount": true

    }

}


const obj2 = {

    "val1": "test",

    "stream": {

        "iscommisonAccount": true,

        "istradeAccount": true

    }

}


const obj3 = {

    "val1": "test",

    "stream": {

        "iscommisonAccount": false,

        "istradeAccount": true

    }

}


const obj4 = {

    "val1": "test",

    "stream": {

        "iscommisonAccount": true,

        "istradeAccount": false

    }

}


const obj5 = {

    "val1":"test",

    "stream":{

       "iscommisonAccount":true,

       "istradeAccount":true

    }

 }

 

 const obj6 = {

    "val1":"test",

    "stream":{

       "iscommisonAccount":true,

       "istradeAccount":true

    }

 }


console.log(findCommonValues(obj1, obj2))

console.log(findCommonValues(obj3, obj4))

console.log(findCommonValues(obj5, obj6))

如果你希望它盡可能小。這確實(shí)是我能做的最好的事情了。


const commonValues = (obj1, obj2) => Object.keys(obj1).reduce((result, key) => obj1[key] && obj1[key] === obj2[key] ? { ...result, [key]: obj1[key] } : typeof obj1[key] === 'object' && obj1[key] !== null ? { ...result, [key]: commonValues(obj1[key], obj2[key]) } : result, {});


const obj1 = {

    "val1": "test",

    "stream": {

        "iscommisonAccount": false,

        "istradeAccount": true

    }

}


const obj2 = {

    "val1": "test",

    "stream": {

        "iscommisonAccount": true,

        "istradeAccount": true

    }

}


const obj3 = {

    "val1": "test",

    "stream": {

        "iscommisonAccount": false,

        "istradeAccount": true

    }

}


const obj4 = {

    "val1": "test",

    "stream": {

        "iscommisonAccount": true,

        "istradeAccount": false

    }

}


const obj5 = {

    "val1": "test",

    "stream": {

        "iscommisonAccount": true,

        "istradeAccount": true

    }

}


const obj6 = {

    "val1": "test",

    "stream": {

        "iscommisonAccount": true,

        "istradeAccount": true

    }

}


console.log(commonValues(obj1, obj2))

console.log(commonValues(obj3, obj4))

console.log(commonValues(obj5, obj6))

TypeScript 版本


export type KeyValueObject = {

    [key: string]: number | boolean | string | KeyValueObject

}


export const isKeyValueObject = (obj1: number | boolean | string | KeyValueObject): obj1 is KeyValueObject => typeof obj1 === 'object' && obj1 !== null;


export const commonValues = (obj1: KeyValueObject, obj2: KeyValueObject): KeyValueObject =>

    Object.keys(obj1).reduce((result, key) =>

        obj1[key] && obj1[key] === obj2[key]

            ? { ...result, [key]: obj1[key] }

            : isKeyValueObject(obj1[key]) && isKeyValueObject(obj2[key])

                ? { ...result, [key]: commonValues(obj1[key] as KeyValueObject, obj2[key] as KeyValueObject) }

                : result,

        {}

    );


查看完整回答
反對(duì) 回復(fù) 2023-10-20
?
慕妹3146593

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊

這是一個(gè)簡(jiǎn)單的示例,它使用 Object.values 和 Object.keys 的組合作為數(shù)組,經(jīng)過過濾以生成指定的輸出:


let obj1 = { "val1":"test", "stream":{ "iscommisonAccount":false, "istradeAccount":true } }; 

let obj2 = { "val1":"test", "stream":{ "iscommisonAccount":true, "istradeAccount":true } };



let obj3 = { "val1":"test", "stream":{ "iscommisonAccount":true, "istradeAccount":true } }; 

let obj4 = { "val1":"test", "stream":{ "iscommisonAccount":false, "istradeAccount":true } };


let obj5 = { "val1":"test", "stream":{ "iscommisonAccount":false, "istradeAccount":false } }; 

let obj6 = { "val1":"test", "stream":{ "iscommisonAccount":false, "istradeAccount":false } };


let obj7 = { "val1":"test", "stream":{ "iscommisonAccount":true, "istradeAccount":false } }; 

let obj8 = { "val1":"test", "stream":{ "iscommisonAccount":true, "istradeAccount":false } };


interface Data {stream:{[key: string]: boolean}};


function objFilter(objA: Data, objB: Data): Data {

  let out: Data = {stream:{}};

  Object.keys(objA.stream).filter((value, idx) =>

    Object.values(objA.stream)[idx] === Object.values(objB.stream)[idx] ? 

      out.stream[value] = Object.values(objA.stream)[idx] :

      false

  );

  return out;

}

console.log(objFilter(obj1, obj2)); //istradeAccount

console.log(objFilter(obj3, obj4)); //istradeAccount

console.log(objFilter(obj5, obj6)); //iscommisonAccount && istradeAccount

console.log(objFilter(obj7, obj8)); //iscommisonAccount && istradeAccount

console.log(objFilter(obj2, obj7)); //iscommisonAccount


查看完整回答
反對(duì) 回復(fù) 2023-10-20
  • 2 回答
  • 0 關(guān)注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)