Javascript 操作对象

希望有人能给我提示来解决我的问题。


我有一个具有以下方案的给定对象:


{

  "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中的数组长度相同(本例中为 2)。我找不到使用 javascript 本机函数重构原始对象的可靠方法。任何帮助表示赞赏。nested1nested2


墨色风雨
浏览 157回答 4
4回答

陪伴而非守候

我稍微修改了你的例子,并使用了字符串,以便有一个有效的对象。这将是一种方法: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);

翻阅古今

如果您是初学者,那么您可以像这样简单地完成上述任务。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]});

qq_遁去的一_1

使用map和解构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));

阿晨1998

任意情况的通用解决方案function buildItems(obj) {&nbsp; const commonPairs = Object.entries(obj).reduce(&nbsp; &nbsp; (accumulate, [key, val]) =>&nbsp; &nbsp; &nbsp; Array.isArray(val) ? accumulate : { ...accumulate, [key]: val },&nbsp; &nbsp; {}&nbsp; )&nbsp; const arrayPairs = Object.entries(obj).reduce(&nbsp; &nbsp; (accumulate, [key, val]) =>&nbsp; &nbsp; &nbsp; !Array.isArray(val) ? accumulate : { ...accumulate, [key]: val },&nbsp; &nbsp; {}&nbsp; )&nbsp; if (Object.keys(arrayPairs).length === 0) {&nbsp; &nbsp; return [{ ...commonPairs }]&nbsp; }&nbsp; const res = []&nbsp; for (let i = 0; i < arrayPairs[Object.keys(arrayPairs)[0]].length; i++) {&nbsp; &nbsp; res.push({&nbsp; &nbsp; &nbsp; ...commonPairs,&nbsp; &nbsp; &nbsp; ...Object.keys(arrayPairs).reduce(&nbsp; &nbsp; &nbsp; &nbsp; (acc, key) => ({ ...acc, [key]: arrayPairs[key][i] }),&nbsp; &nbsp; &nbsp; &nbsp; {}&nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; })&nbsp; }&nbsp; return res}console.log(&nbsp; "1)",&nbsp; buildItems({&nbsp; &nbsp; prop1: "value1",&nbsp; &nbsp; prop2: "value2",&nbsp; &nbsp; nested1: [{ A: 1 }, { B: 1 }, { G: 1 }],&nbsp; &nbsp; nested2: [{ C: 1 }, { D: 1 }, { H: 1 }],&nbsp; &nbsp; nested3: [{ E: 1 }, { F: 1 }, { I: 1 }],&nbsp; &nbsp; nested4: [{ J: 1 }, { K: 1 }, { L: 1 }],&nbsp; &nbsp; nested5: [{ M: 1 }, { N: 1 }, { O: 1 }],&nbsp; }),&nbsp; "\n")console.log(&nbsp; "2)",&nbsp; buildItems({&nbsp; &nbsp; prop1: "value1",&nbsp; &nbsp; prop2: "value2",&nbsp; }),&nbsp; "\n")console.log(&nbsp; "3)",&nbsp; buildItems({&nbsp; &nbsp; prop1: "value1",&nbsp; &nbsp; prop2: "value2",&nbsp; &nbsp; nested1: [{ A: 1 }, { B: 1 }, { G: 1 }],&nbsp; }),&nbsp; "\n")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript