简体   繁体   中英

Es6 way to convert object key value to one single object

I want to convert all data into one object,

let d = {
  "Coupon_Code": "code",
  "Coupon_Name": "namie",
  "Coupon_Desc": 1000,
  "selectedCity": [
    {
      "Coupon_City_Name": "xyz"
    }
  ],
  "selectedCategory": [
    {
      "Coupon_Category_Name": "Shopping"
    }
  ],
  "selectedCompany": [
    {
      "Coupon_Company_Name": "Shopper Stop"
    }
  ],
  "selectedState": [
    {
      "Coupon_State_Name": "abc"
    }
  ],
  "Coupon_Date": "2222-02-22",
}

i tried some methods of Object like keys , entries but dont no what to use. Final output should be

let d = {
  Coupon_Code: "code",
  Coupon_Name: "namie",
  Coupon_Desc: 1000,
  Coupon_City_Name: "xyz",
  Coupon_Category_Name: "Shopping",
  Coupon_Company_Name: "Shopper Stop",
  Coupon_State_Name: "abc",
  Coupon_Date: "2222-02-22",
};

what's the best and optimum way to have above result using Venila Js and Es6

Reduce the entries of the original object. If the entry's value is an array merge the 1st element, if not merge the original key and value. You can merge the properties into the object using object spread :

 const data = {"Coupon_Code":"code","Coupon_Name":"namie","Coupon_Desc":1000,"selectedCity":[{"Coupon_City_Name":"xyz"}],"selectedCategory":[{"Coupon_Category_Name":"Shopping"}],"selectedCompany":[{"Coupon_Company_Name":"Shopper Stop"}],"selectedState":[{"Coupon_State_Name":"abc"}],"Coupon_Date":"2222-02-22"}; const result = Object.entries(data) .reduce((r, [k, v]) => ({ ...r, ...Array.isArray(v) ? v[0] : { [k]: v } }), {}); console.log(result); 

You can use Array.reduce and Object.entries

 let d = {"Coupon_Code":"code","Coupon_Name":"namie","Coupon_Desc":1000,"selectedCity":[{"Coupon_City_Name":"xyz"}],"selectedCategory":[{"Coupon_Category_Name":"Shopping"}],"selectedCompany":[{"Coupon_Company_Name":"Shopper Stop"}],"selectedState":[{"Coupon_State_Name":"abc"}],"Coupon_Date":"2222-02-22"}; d = Object.entries(d).reduce((a,[k,v]) => { // If the value is an array, iterate over it to merge into the resultant object if(Array.isArray(v)) Object.assign(a, ...v) else Object.assign(a, {[k]:v}) // if it is not an array, merge into resultant object return a; }, {}); console.log(d); 

You could take a recursive approach.

 const fn = o => Object.assign(...Object.entries(o).map(([k, v]) => Array.isArray(v) ? Object.assign(...v.map(fn)) : { [k]: v })), d = { Coupon_Code: "code", Coupon_Name: "namie", Coupon_Desc: 1000, selectedCity: [{ Coupon_City_Name: "xyz" }], selectedCategory: [{ Coupon_Category_Name: "Shopping" }], selectedCompany: [{ Coupon_Company_Name: "Shopper Stop" }], selectedState: [{ Coupon_State_Name: "abc" }], Coupon_Date: "2222-02-22" }, result = fn(d); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

A possible iterative solution is:

function flatten(obj) {
    let r = {}
    for (let [key, value] of Object.entries(obj)) {
        if (Array.isArray(value)) {
            Object.assign(r, value[0]);
        } else {
            Object.assign(r, {[key]: value});
        }
    }
    return r;
}

Something like this:

 const d = { Coupon_Code: "code", Coupon_Name: "namie", Coupon_Desc: 1000, selectedCity: [{ Coupon_City_Name: "xyz" }], selectedCategory: [{ Coupon_Category_Name: "Shopping" }], selectedCompany: [{ Coupon_Company_Name: "Shopper Stop" }], selectedState: [{ Coupon_State_Name: "abc" }], Coupon_Date: "2222-02-22" }; function toSingleObj(obj) { var result = {}; Object.entries(obj).forEach(([key,value]) => { if (Array.isArray(value)) { Object.entries(value[0]).forEach(([k,v]) => { result[k] = v; }); } else { result[key] = value; } }); return result; } console.log("Result: ", toSingleObj(d)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM