2

I'm not familiar with javascript and I don't know how to search for this problem. Here I've explained briefly about my problem. Below is the data from which I've to find out the similar entries in the data and combine them as one object. This is my data:

var myArr = [{
        "Company": "Samsung",
        "model": "Galaxy S2",
        "screen_size": "5.5"
    },
    {
        "Company": "Samsung",
        "model": "Galaxy S3",
        "screen_size": "5.5"
    },
    {
        "Company": "Samsung",
        "model": "Galaxy S4",
        "screen_size": "5.5"
    },
    {
        "Company": "Xiaomi",
        "model": "Redmi 2",
        "screen_size": "4.7"
    },
    {
        "Company": "Xiaomi",
        "model": "Redmi 4",
        "screen_size": "5"
    },
    {
        "Company": "Xiaomi",
        "model": "Redmi Note 4",
        "screen_size": "5.5"
    }
]

I want to combine all the duplicate entries in the array. So, my outcome of this data should be like this:

[{
    "Company": "Samsung",
    "models": [{
        "Galaxy S2": {
            "screen_size": "5.5"
        }
    }, {
        "Galaxy S3": {
            "screen_size": "5.6"
        }
    }, {
        "Galaxy S4": {
            "screen_size": "5.7"
        }
    }]
}, {
    "Company": "Xiaomi",
    "models": [{
        "Redmi 2": {
            "screen_size": "4.7"
        }
    }, {
        "Redmi 4": {
            "screen_size": "5"
        }
    }, {
        "Redmi Note 4": {
            "screen_size": "5.5"
        }
    }]
}]

Somebody, please help me.

2 Answers 2

2

You can group your object using array#reduce on the Company key in an object and extract all the values using Object.values().

var myArr=[{ "Company": "Samsung", "model": "Galaxy S2", "screen_size": "5.5" }, { "Company": "Samsung", "model": "Galaxy S3", "screen_size": "5.5" }, { "Company": "Samsung", "model": "Galaxy S4", "screen_size": "5.5" }, { "Company": "Xiaomi", "model":"Redmi 2", "screen_size": "4.7" }, { "Company": "Xiaomi", "model": "Redmi 4", "screen_size": "5" }, { "Company": "Xiaomi", "model": "Redmi Note 4", "screen_size": "5.5" } ],
    result = Object.values(myArr.reduce((r,o) => {
      r[o.Company] = r[o.Company] || {'Company': o.Company, 'Models' : []};
      r[o.Company]['Models'].push({[o.model]:{'screen_size' : o.screen_size}});
      return r;
    }, {}));

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

0
const data = [
  { first_name: 'Hamo', last_name: 'Muradyan', subject: 'Chemistry', score: 35 },
  { first_name: 'Aren', last_name: 'Abelyan', subject: 'Math', score: 90 },
  { first_name: 'John', last_name: 'Doe', subject: 'Geometry', score: 24 },
  { first_name: 'Vardges', last_name: 'Gasparyan', subject: 'Math', score: 10 },
  { first_name: 'Hambardzum', last_name: 'Simonyan', subject: 'Astronomy', score: 80 },
  { first_name: 'Kamo', last_name: 'Hakhverdyan', subject: 'Physics', score: 21 },
  { first_name: 'Ashot', last_name: 'Vardanyan', subject: 'Chemistry', score: 28 },
  { first_name: 'Vardges', last_name: 'Mnoyan', subject: 'Physics', score: 100 },
  { first_name: 'Ruzanna', last_name: 'Sargsyan', subject: 'Astronomy', score: 98 },
  { first_name: 'Hasmik', last_name: 'Kchepyan', subject: 'Math', score: 99 },
  { first_name: 'Ani', last_name: 'Hovhannisyan', subject: 'Psychology', score: 100 },
  { first_name: 'Kima', last_name: 'Ghazaryan', subject: 'Math', score: 88 },
  { first_name: 'Karen', last_name: 'Manukyan', subject: 'Psychology', score: 1 },
  { first_name: 'Manuk', last_name: 'Tserunyan', subject: 'Astronomy', score: 65 },
];

function getResult () {
  let obj = {};
  for(let i = 0; i < data.length; i++) {
    obj[data[i].subject] = obj[data[i].subject] || {subject: data[i].subject, persons: []};
    obj[data[i].subject].persons.push(data[i]);
  };
  
  return Object.values(obj);
};

getResult();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.