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

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

如何將單個(gè)鍵值對(duì)對(duì)象轉(zhuǎn)換為具有鍵值對(duì)的對(duì)象數(shù)組?

如何將單個(gè)鍵值對(duì)對(duì)象轉(zhuǎn)換為具有鍵值對(duì)的對(duì)象數(shù)組?

千萬里不及你 2023-03-24 16:26:28
我有一個(gè)鍵值對(duì)的單個(gè)對(duì)象,每個(gè)鍵都有一個(gè)數(shù)組作為值,如何通過將它與另一個(gè)對(duì)象數(shù)組進(jìn)行比較,將其轉(zhuǎn)換為具有鍵值對(duì)屬性的對(duì)象數(shù)組。var keys = [];for (var k in obj) keys.push(k);輸入:obj =  {          "name": "jack",          "age": 10,          "country": "india",          "state": "Delhi"        }        obj2 = [{key: "product", type:"", value:"", required:"true", mandatory:"true"},        {key: "sub-product", type:"", value:"", required:"true", mandatory:"true"},        {key: "name", type:"text", value:"", required:"true", mandatory:"true"},        {key: "age", type:"text", value:"[0-9]", required:"true", mandatory:"true"},        {key: "country", type:"text", value:"[a-z]", required:"true", mandatory:"true"},        {key: "state", type:"text", value:"[a-z]", required:"true", mandatory:"true"}]預(yù)期輸出:result = [{key: "name", type:"text", value:"", required:"true", mandatory:"true",setValue:"jack"},        {key: "age", type:"text", value:"[0-9]", required:"true", mandatory:"true",setValue:"10"},        {key: "country", type:"text", value:"[a-z]", required:"true", mandatory:"true",setValue:"india"},        {key: "state", type:"text", value:"[a-z]", required:"true", mandatory:"true",setValue:"Delhi"}];
查看完整描述

3 回答

?
ABOUTYOU

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

循環(huán)結(jié)束obj2。如果該key值是 的屬性obj,則將相應(yīng)的值復(fù)制到setValue對(duì)象的屬性中,并將其壓入結(jié)果。


obj = {

  "name": "jack",

  "age": 10,

  "country": "india",

  "state": "Delhi"

};

obj2 = [{key: "product", type="", value="", required="true", mandatory="true"},

        {key: "sub-product", type="", value="", required="true", mandatory="true"},

        {key: "name", type="text", value="", required="true", mandatory="true"},

        {key: "age", type="text", value="[0-9]", required="true", mandatory="true"},

        {key: "country", type="text", value="[a-z]", required="true", mandatory="true"},

        {key: "state", type="text", value="[a-z]", required="true", mandatory="true"}];


var result = [];

obj2.forEach(o => {

  if (o.key in obj) {

    o.setValue = obj[o.key];

    result.push(o);

  }

});

console.log(result);


查看完整回答
反對(duì) 回復(fù) 2023-03-24
?
LEATH

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

您可以先使用過濾它hasOwnProperty,然后將其映射到設(shè)置setValue屬性。這是實(shí)現(xiàn):


var obj = { "name": "jack", "age": 10, "country": "india", "state": "Delhi" };

var obj2 =[{key: "product", type:"", value:"", required:"true", mandatory:"true"}, {key: "sub-product", type:"", value:"", required:"true", mandatory:"true"}, {key: "name", type:"text", value:"", required:"true", mandatory:"true"}, {key: "age", type:"text", value:"[0-9]", required:"true", mandatory:"true"}, {key: "country", type:"text", value:"[a-z]", required:"true", mandatory:"true"}, {key: "state", type:"text", value:"[a-z]", required:"true", mandatory:"true"}];



var result = obj2.filter(k=>obj.hasOwnProperty(k.key)).map(p=>({...p, setValue:obj[p.key]}));


console.log(result);


查看完整回答
反對(duì) 回復(fù) 2023-03-24
?
撒科打諢

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

你可以使用Array.prototype.flatmap來實(shí)現(xiàn)這個(gè)

const obj = {

  "name": "jack",

  "age": 10,

  "country": "india",

  "state": "Delhi"

}

const obj2 = [

  {key: "product", type:"", value:"", required:"true", mandatory:"true"},

  {key: "sub-product", type:"", value:"", required:"true", mandatory:"true"},

  {key: "name", type:"text", value:"", required:"true", mandatory:"true"},

  {key: "age", type:"text", value:"[0-9]", required:"true", mandatory:"true"},

  {key: "country", type:"text", value:"[a-z]", required:"true", mandatory:"true"},

  {key: "state", type:"text", value:"[a-z]", required:"true", mandatory:"true"}

]


const result = obj2.flatMap((entry) => {

  if (obj[entry.key]) {

    return {

      ...entry,

            setValue: obj[entry.key],

    }

  }

    return []

})


console.log(result)



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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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