最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Transform javascript object - Stack Overflow

programmeradmin2浏览0评论

This simple (as before, I thought) transformation of a JavaScript object defeats me. I would like to flatten this object in the following way.

This is what I have:

{
    "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",
}}

I tried using .reduce() or .map() but ineffectively.

This simple (as before, I thought) transformation of a JavaScript object defeats me. I would like to flatten this object in the following way.

This is what I have:

{
    "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",
}}

I tried using .reduce() or .map() but ineffectively.

Share Improve this question edited Nov 2, 2018 at 7:19 Roy Scheffers 3,90811 gold badges33 silver badges36 bronze badges asked Nov 2, 2018 at 6:48 Elly KedwardElly Kedward 511 gold badge1 silver badge2 bronze badges 3
  • 1 The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific problem you're having in a minimal reproducible example. For more information, please see How to Ask and take the tour. – CertainPerformance Commented Nov 2, 2018 at 6:51
  • It looks like you've copied the data from a debugger somewhere, you might want to clarify the object/array structure there. What you have in your first block isn't valid JSON (or hjavascript). – Evan Trimboli Commented Nov 2, 2018 at 6:53
  • 2 The Object data is incorrect.. Correct please – Yosvel Quintero Commented Nov 2, 2018 at 6:53
Add a ment  | 

3 Answers 3

Reset to default 2

You can do:

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; }

This Object is kind of weird like array-like objects, but index start from 1 not 0, and lack of length.

Fix some typo first, brand should be object not 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))

 

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)

发布评论

评论列表(0)

  1. 暂无评论