1

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

5 Answers 5

7

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

2

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

0
2

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

2
  • ... Object.assign(...v.map(fn)) ... is calling itselft with fn. Sep 29, 2018 at 18:44
  • Oh my bad. Didn't see that. Rather elegant. Sep 29, 2018 at 18:50
1

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.