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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript 操作對象

Javascript 操作對象

墨色風雨 2022-12-18 16:27:54
希望有人能給我提示來解決我的問題。我有一個具有以下方案的給定對象:{  "prop1": value1,  "prop2": value2,  "nested1": [{A},{B}],  "nested2": [{C},{D}],  "nested3": [{E},{F}],}我想從我的原始對象中得到的是:{    items: [        {            "prop1": value1,            "prop2": value2,            "nested1": {A},            "nested2": {C},            "nested3": {E},        },        {            "prop1": value1,            "prop2": value2,            "nested1": {B},            "nested2": {D},            "nested3": {F},        },          ]}這與原始對象等屬性items.length中的數(shù)組長度相同(本例中為 2)。我找不到使用 javascript 本機函數(shù)重構(gòu)原始對象的可靠方法。任何幫助表示贊賞。nested1nested2
查看完整描述

4 回答

?
陪伴而非守候

TA貢獻1757條經(jīng)驗 獲得超8個贊

我稍微修改了你的例子,并使用了字符串,以便有一個有效的對象。這將是一種方法:


const input = {

  prop1: "value1",

  prop2: "value2",

  nested1: ["A","B"],

  nested2: ["C","D"],

  nested3: ["E","F"],

};


const output = new Array(input.nested1.length).fill().map((_, i) => ({

    prop1: input.prop1,

    prop2: input.prop2,

    nested1: input.nested1[i],

    nested2: input.nested2[i],

    nested3: input.nested3[i]

}));


console.log(output);


查看完整回答
反對 回復 2022-12-18
?
翻閱古今

TA貢獻1780條經(jīng)驗 獲得超5個贊

如果您是初學者,那么您可以像這樣簡單地完成上述任務(wù)。


const input = {

  prop1: "value1",

  prop2: "value2",

  nested1: ["A","B"],

  nested2: ["C","D"],

  nested3: ["E","F"],

};


let arr1 ={

  prop1:input.prop1,

  prop2:input.prop2,

  nested1:input.nested1[0],

  nestend2:input.nested2[0],

  nested3:input.nested3[0],

}

let arr2 ={

  prop1:input.prop1,

  prop2:input.prop2,

  nested1:input.nested1[1],

  nestend2:input.nested2[1],

  nested3:input.nested3[1],

}


console.log({items:[arr1,arr2]});


查看完整回答
反對 回復 2022-12-18
?
qq_遁去的一_1

TA貢獻1725條經(jīng)驗 獲得超8個贊

使用map和解構(gòu)


const convert = ({ nested1, nested2, nested3, ...rest }) => ({

  items: nested1.map((nested1, i) => ({

    ...rest,

    nested1,

    nested2: nested2[i],

    nested3: nested3[i],

  }))

});


const obj = {

  prop1: 'value1',

  prop2: 'value2',

  nested1: [{ 'A': 'a' }, { 'B': 'b' }],

  nested2: [{ 'C': 1 }, { 'D': 2 }],

  nested3: [{ 'E': 5 }, { 'F': 6 }],

};


console.log(convert(obj));


查看完整回答
反對 回復 2022-12-18
?
阿晨1998

TA貢獻2037條經(jīng)驗 獲得超6個贊

任意情況的通用解決方案


function buildItems(obj) {

  const commonPairs = Object.entries(obj).reduce(

    (accumulate, [key, val]) =>

      Array.isArray(val) ? accumulate : { ...accumulate, [key]: val },

    {}

  )

  const arrayPairs = Object.entries(obj).reduce(

    (accumulate, [key, val]) =>

      !Array.isArray(val) ? accumulate : { ...accumulate, [key]: val },

    {}

  )


  if (Object.keys(arrayPairs).length === 0) {

    return [{ ...commonPairs }]

  }


  const res = []

  for (let i = 0; i < arrayPairs[Object.keys(arrayPairs)[0]].length; i++) {

    res.push({

      ...commonPairs,

      ...Object.keys(arrayPairs).reduce(

        (acc, key) => ({ ...acc, [key]: arrayPairs[key][i] }),

        {}

      ),

    })

  }


  return res

}


console.log(

  "1)",

  buildItems({

    prop1: "value1",

    prop2: "value2",

    nested1: [{ A: 1 }, { B: 1 }, { G: 1 }],

    nested2: [{ C: 1 }, { D: 1 }, { H: 1 }],

    nested3: [{ E: 1 }, { F: 1 }, { I: 1 }],

    nested4: [{ J: 1 }, { K: 1 }, { L: 1 }],

    nested5: [{ M: 1 }, { N: 1 }, { O: 1 }],

  }),

  "\n"

)


console.log(

  "2)",

  buildItems({

    prop1: "value1",

    prop2: "value2",

  }),

  "\n"

)


console.log(

  "3)",

  buildItems({

    prop1: "value1",

    prop2: "value2",

    nested1: [{ A: 1 }, { B: 1 }, { G: 1 }],

  }),

  "\n"

)


查看完整回答
反對 回復 2022-12-18
  • 4 回答
  • 0 關(guān)注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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