将JavaScript对象转换

3
这个JavaScript对象的简单转换(我之前认为)让我无法应对。我想以以下方式展开这个对象。 这是我的原始对象:
{
    "1": {
        "group": "Clothes",
        "brand": {
            "0": {
                "brand_id": "12",
                "brand_name": "Adidas"
            },
            "1": {
                "brand_id": "15",
                "brand_name": "Zara"
            }
        }
    },
    "2": {
        "group": "Cars",
        "brand": {
            "0": {
                "brand_id": "43",
                "brand_name": "Ferrari"
            },
            "1": {
                "brand_id": "51",
                "brand_name": "BMW"
            }
        }
    }
}

And this is it, what I want to get

{0: {
    brand_id: "12",
    brand_name: "Adidas",
    group: "Clothes",
}
1: {
    brand_id: "15",
    brand_name: "Zara",
    group: "Clothes",
},
2: {
    brand_id: "43",
    brand_name: "Ferrari",
    group: "Cars",
}
3: {
    brand_id: "51",
    brand_name: "BMW",
    group: "Cars",
}}

我尝试使用.reduce().map(),但效果不佳。


1
发布的问题似乎没有任何尝试来解决问题。StackOverflow希望你首先尝试解决自己的问题,因为你的尝试可以帮助我们更好地理解你的需求。请编辑问题以展示你所尝试的内容,以便说明你遇到的具体问题(请使用MCVE)。如需更多信息,请参阅提问指南并参观网站导览。 - undefined
看起来你从调试器的某个地方复制了数据,你可能需要澄清对象/数组的结构。你在第一个代码块中的内容不是有效的JSON(或hjavascript)。 - undefined
2
对象数据不正确。请进行更正。 - undefined
3个回答

2

您可以做以下事情:

const data = {1: {group: 'Clothes',brand: [{0: {brand_id: '12',brand_name: 'Adidas'}},{1: {brand_id: '15',brand_name: 'Zara'}}]},2: {group: 'Cars',brand: [{0: {brand_id: '43',brand_name: 'Ferrari'}},{1: {brand_id: '51',brand_name: 'BMW'}}]}};
const result = {};

Object.keys(data).forEach(k => {
  data[k].brand.forEach((b, i) => {
    result[Object.keys(result).length] = {
      brand_id: b[i].brand_id,
      brand_name: b[i].brand_name,
      group: data[k].group
    };
  });
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }


1
这个对象有点像“类数组对象”,但索引从1开始而不是0,并且缺少“length”属性。 首先修复一些拼写错误,“brand”应该是“object”而不是“array”。

var objectOri = {
    1: {
        group: 'Clothes',
        brand: {
            0: {
                brand_id: "12",
                brand_name: "Adidas"
            },
            1: {
                brand_id: "15",
                brand_name: "Zara"
            }
        }
    },
    2: {
        group: 'Cars',
        brand: {
            0: {
                brand_id: "43",
                brand_name: "Ferrari"
            },
            1: {
                brand_id: "51",
                brand_name: "BMW"
            }
        }
    }
};

let arr = [];
Object.keys(objectOri).forEach(index => {
    let brand = objectOri[index].brand;
    Object.keys(brand).forEach(brIndex => {
        arr.push({
            ...brand[brIndex],
            group: objectOri[index].group
        });
    });
});


console.log(Object.assign({}, arr))

 

Also, if this obejct is a array-like object, the key should start from 0 not 1, and length is also necessary, like below.

var objectOri = {
    0: {
        group: 'Clothes',
        brand: {
            0: {
                brand_id: "12",
                brand_name: "Adidas"
            },
            1: {
                brand_id: "15",
                brand_name: "Zara"
            },
            length: 2
        }
    },
    1: {
        group: 'Cars',
        brand: {
            0: {
                brand_id: "43",
                brand_name: "Ferrari"
            },
            1: {
                brand_id: "51",
                brand_name: "BMW"
            },
            length: 2
        }
    },
    length: 2
};

let arr = [];
Array.from(objectOri).forEach(item => {
    Array.from(item.brand).forEach(brand => {
        arr.push({
            ...brand,
            group: item.group
        });
    });
});


console.log(Object.assign({}, arr))

 


0

let input = { "1": { "group": "Clothes", "brand": { "0": { "brand_id": "12", "brand_name": "Adidas"}, "1": { "brand_id": "15",                    "brand_name": "Zara"}} },
              "2": { "group": "Cars", "brand": { "0": { "brand_id": "43","brand_name": "Ferrari"},                "1": { "brand_id": "51", "brand_name": "BMW"}}}}

output = {}
Object.values(input).forEach(({ group, brand })=> {
  Object.values(brand).forEach(({ brand_id, brand_name })=>{      
    output[Object.keys(output).length]= {
      brand_id,
      brand_name,
      group,
    }
  })
})

console.log(output)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,