How to merge objects that's inside an array using native javascript
I have an api that gives me the right data, but it's mostly the same data in each object. I want to restructure the objects in the array into one object.
This is the data that my api products currently.
var data = [
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives
Accessories"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven
Abrasives"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded
Abrasives"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated
Abrasives"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty
Abrasives"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Super
Abrasives"}
];
var transformed= data.map(function(obj){
var result= {
key: obj.maincategory,
values:
}
console.log(result)
for (var key in obj) {
if (obj.hasOwnProperty(key) && key !== "maincategory") {
result.values.push([key, obj[key]]);
}
}
return result;
});
console.log(transformed);
This is how I would like the data to be structured.
var data = [
{
maincategory: "AP",
maincatname: "Abrasives",
subcatname: ['Abrasives Accessories','Non-woven Abrasives','Bonded Abrasives','Wire Brushes', etc]
},
];
javascript arrays data-structures javascript-objects
add a comment |
I have an api that gives me the right data, but it's mostly the same data in each object. I want to restructure the objects in the array into one object.
This is the data that my api products currently.
var data = [
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives
Accessories"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven
Abrasives"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded
Abrasives"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated
Abrasives"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty
Abrasives"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Super
Abrasives"}
];
var transformed= data.map(function(obj){
var result= {
key: obj.maincategory,
values:
}
console.log(result)
for (var key in obj) {
if (obj.hasOwnProperty(key) && key !== "maincategory") {
result.values.push([key, obj[key]]);
}
}
return result;
});
console.log(transformed);
This is how I would like the data to be structured.
var data = [
{
maincategory: "AP",
maincatname: "Abrasives",
subcatname: ['Abrasives Accessories','Non-woven Abrasives','Bonded Abrasives','Wire Brushes', etc]
},
];
javascript arrays data-structures javascript-objects
add a comment |
I have an api that gives me the right data, but it's mostly the same data in each object. I want to restructure the objects in the array into one object.
This is the data that my api products currently.
var data = [
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives
Accessories"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven
Abrasives"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded
Abrasives"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated
Abrasives"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty
Abrasives"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Super
Abrasives"}
];
var transformed= data.map(function(obj){
var result= {
key: obj.maincategory,
values:
}
console.log(result)
for (var key in obj) {
if (obj.hasOwnProperty(key) && key !== "maincategory") {
result.values.push([key, obj[key]]);
}
}
return result;
});
console.log(transformed);
This is how I would like the data to be structured.
var data = [
{
maincategory: "AP",
maincatname: "Abrasives",
subcatname: ['Abrasives Accessories','Non-woven Abrasives','Bonded Abrasives','Wire Brushes', etc]
},
];
javascript arrays data-structures javascript-objects
I have an api that gives me the right data, but it's mostly the same data in each object. I want to restructure the objects in the array into one object.
This is the data that my api products currently.
var data = [
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives
Accessories"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven
Abrasives"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded
Abrasives"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated
Abrasives"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty
Abrasives"},
{maincategory: "AP", maincatname: "Abrasives", subcatname: "Super
Abrasives"}
];
var transformed= data.map(function(obj){
var result= {
key: obj.maincategory,
values:
}
console.log(result)
for (var key in obj) {
if (obj.hasOwnProperty(key) && key !== "maincategory") {
result.values.push([key, obj[key]]);
}
}
return result;
});
console.log(transformed);
This is how I would like the data to be structured.
var data = [
{
maincategory: "AP",
maincatname: "Abrasives",
subcatname: ['Abrasives Accessories','Non-woven Abrasives','Bonded Abrasives','Wire Brushes', etc]
},
];
javascript arrays data-structures javascript-objects
javascript arrays data-structures javascript-objects
asked Nov 21 '18 at 16:49
Eric Evans
289624
289624
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can use array#reduce
with Object.values()
. Create an object lookup with maincategory
and add maincategory
, maincatname
and subcatname
corresponding to it and then take out values using Object.values()
from this object.
var data = [ {maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives Accessories"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven Abrasives"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded Abrasives"},{maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated Abrasives"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty Abrasives"}, {maincategory:"AP", maincatname: "Abrasives", subcatname: "Super Abrasives"} ]
result = Object.values(data.reduce((r, {maincategory, maincatname, subcatname}) => {
r[maincategory] = r[maincategory] || {maincategory, maincatname, subcatname : };
r[maincategory].subcatname.push(subcatname);
return r;
},{}));
console.log(result);
add a comment |
If you are sure that it's always the same data then you can have your result by using this function:
function transform(data){
obj = {};
obj['maincategory'] = data[0]['maincategory'];
obj['maincatname'] = data[0]['maincatname'];
obj['subcatnames'] = data.map(function(obj){
return obj.subcatname});
return [obj]
}
To be honest the best solution is to fix the api
to return a more structured results.
Thanks for the answers, while all the answers provided the same output. This answer was the most simple to understand. Thank everyone for the answers.
– Eric Evans
Nov 22 '18 at 5:44
add a comment |
You can simply use Array.reduce
to get that output:
var data = [{ maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives Accessories" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Super Abrasives" }]
const r = data.reduce((r,c) => (r.subcatname = [...Array.isArray(r.subcatname)
? r.subcatname
: [r.subcatname], c.subcatname], r))
console.log(r)
The idea is since you only need the subcatname
to be combined you can just use the Array.reduce
and its basic form where it essentially acts as a merge
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416917%2fhow-to-merge-objects-thats-inside-an-array-using-native-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use array#reduce
with Object.values()
. Create an object lookup with maincategory
and add maincategory
, maincatname
and subcatname
corresponding to it and then take out values using Object.values()
from this object.
var data = [ {maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives Accessories"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven Abrasives"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded Abrasives"},{maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated Abrasives"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty Abrasives"}, {maincategory:"AP", maincatname: "Abrasives", subcatname: "Super Abrasives"} ]
result = Object.values(data.reduce((r, {maincategory, maincatname, subcatname}) => {
r[maincategory] = r[maincategory] || {maincategory, maincatname, subcatname : };
r[maincategory].subcatname.push(subcatname);
return r;
},{}));
console.log(result);
add a comment |
You can use array#reduce
with Object.values()
. Create an object lookup with maincategory
and add maincategory
, maincatname
and subcatname
corresponding to it and then take out values using Object.values()
from this object.
var data = [ {maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives Accessories"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven Abrasives"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded Abrasives"},{maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated Abrasives"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty Abrasives"}, {maincategory:"AP", maincatname: "Abrasives", subcatname: "Super Abrasives"} ]
result = Object.values(data.reduce((r, {maincategory, maincatname, subcatname}) => {
r[maincategory] = r[maincategory] || {maincategory, maincatname, subcatname : };
r[maincategory].subcatname.push(subcatname);
return r;
},{}));
console.log(result);
add a comment |
You can use array#reduce
with Object.values()
. Create an object lookup with maincategory
and add maincategory
, maincatname
and subcatname
corresponding to it and then take out values using Object.values()
from this object.
var data = [ {maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives Accessories"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven Abrasives"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded Abrasives"},{maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated Abrasives"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty Abrasives"}, {maincategory:"AP", maincatname: "Abrasives", subcatname: "Super Abrasives"} ]
result = Object.values(data.reduce((r, {maincategory, maincatname, subcatname}) => {
r[maincategory] = r[maincategory] || {maincategory, maincatname, subcatname : };
r[maincategory].subcatname.push(subcatname);
return r;
},{}));
console.log(result);
You can use array#reduce
with Object.values()
. Create an object lookup with maincategory
and add maincategory
, maincatname
and subcatname
corresponding to it and then take out values using Object.values()
from this object.
var data = [ {maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives Accessories"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven Abrasives"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded Abrasives"},{maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated Abrasives"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty Abrasives"}, {maincategory:"AP", maincatname: "Abrasives", subcatname: "Super Abrasives"} ]
result = Object.values(data.reduce((r, {maincategory, maincatname, subcatname}) => {
r[maincategory] = r[maincategory] || {maincategory, maincatname, subcatname : };
r[maincategory].subcatname.push(subcatname);
return r;
},{}));
console.log(result);
var data = [ {maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives Accessories"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven Abrasives"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded Abrasives"},{maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated Abrasives"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty Abrasives"}, {maincategory:"AP", maincatname: "Abrasives", subcatname: "Super Abrasives"} ]
result = Object.values(data.reduce((r, {maincategory, maincatname, subcatname}) => {
r[maincategory] = r[maincategory] || {maincategory, maincatname, subcatname : };
r[maincategory].subcatname.push(subcatname);
return r;
},{}));
console.log(result);
var data = [ {maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives Accessories"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven Abrasives"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded Abrasives"},{maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated Abrasives"}, {maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty Abrasives"}, {maincategory:"AP", maincatname: "Abrasives", subcatname: "Super Abrasives"} ]
result = Object.values(data.reduce((r, {maincategory, maincatname, subcatname}) => {
r[maincategory] = r[maincategory] || {maincategory, maincatname, subcatname : };
r[maincategory].subcatname.push(subcatname);
return r;
},{}));
console.log(result);
answered Nov 21 '18 at 16:55
Hassan Imam
11.5k31230
11.5k31230
add a comment |
add a comment |
If you are sure that it's always the same data then you can have your result by using this function:
function transform(data){
obj = {};
obj['maincategory'] = data[0]['maincategory'];
obj['maincatname'] = data[0]['maincatname'];
obj['subcatnames'] = data.map(function(obj){
return obj.subcatname});
return [obj]
}
To be honest the best solution is to fix the api
to return a more structured results.
Thanks for the answers, while all the answers provided the same output. This answer was the most simple to understand. Thank everyone for the answers.
– Eric Evans
Nov 22 '18 at 5:44
add a comment |
If you are sure that it's always the same data then you can have your result by using this function:
function transform(data){
obj = {};
obj['maincategory'] = data[0]['maincategory'];
obj['maincatname'] = data[0]['maincatname'];
obj['subcatnames'] = data.map(function(obj){
return obj.subcatname});
return [obj]
}
To be honest the best solution is to fix the api
to return a more structured results.
Thanks for the answers, while all the answers provided the same output. This answer was the most simple to understand. Thank everyone for the answers.
– Eric Evans
Nov 22 '18 at 5:44
add a comment |
If you are sure that it's always the same data then you can have your result by using this function:
function transform(data){
obj = {};
obj['maincategory'] = data[0]['maincategory'];
obj['maincatname'] = data[0]['maincatname'];
obj['subcatnames'] = data.map(function(obj){
return obj.subcatname});
return [obj]
}
To be honest the best solution is to fix the api
to return a more structured results.
If you are sure that it's always the same data then you can have your result by using this function:
function transform(data){
obj = {};
obj['maincategory'] = data[0]['maincategory'];
obj['maincatname'] = data[0]['maincatname'];
obj['subcatnames'] = data.map(function(obj){
return obj.subcatname});
return [obj]
}
To be honest the best solution is to fix the api
to return a more structured results.
answered Nov 21 '18 at 17:04
Mohammed Abuiriban
1905
1905
Thanks for the answers, while all the answers provided the same output. This answer was the most simple to understand. Thank everyone for the answers.
– Eric Evans
Nov 22 '18 at 5:44
add a comment |
Thanks for the answers, while all the answers provided the same output. This answer was the most simple to understand. Thank everyone for the answers.
– Eric Evans
Nov 22 '18 at 5:44
Thanks for the answers, while all the answers provided the same output. This answer was the most simple to understand. Thank everyone for the answers.
– Eric Evans
Nov 22 '18 at 5:44
Thanks for the answers, while all the answers provided the same output. This answer was the most simple to understand. Thank everyone for the answers.
– Eric Evans
Nov 22 '18 at 5:44
add a comment |
You can simply use Array.reduce
to get that output:
var data = [{ maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives Accessories" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Super Abrasives" }]
const r = data.reduce((r,c) => (r.subcatname = [...Array.isArray(r.subcatname)
? r.subcatname
: [r.subcatname], c.subcatname], r))
console.log(r)
The idea is since you only need the subcatname
to be combined you can just use the Array.reduce
and its basic form where it essentially acts as a merge
.
add a comment |
You can simply use Array.reduce
to get that output:
var data = [{ maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives Accessories" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Super Abrasives" }]
const r = data.reduce((r,c) => (r.subcatname = [...Array.isArray(r.subcatname)
? r.subcatname
: [r.subcatname], c.subcatname], r))
console.log(r)
The idea is since you only need the subcatname
to be combined you can just use the Array.reduce
and its basic form where it essentially acts as a merge
.
add a comment |
You can simply use Array.reduce
to get that output:
var data = [{ maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives Accessories" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Super Abrasives" }]
const r = data.reduce((r,c) => (r.subcatname = [...Array.isArray(r.subcatname)
? r.subcatname
: [r.subcatname], c.subcatname], r))
console.log(r)
The idea is since you only need the subcatname
to be combined you can just use the Array.reduce
and its basic form where it essentially acts as a merge
.
You can simply use Array.reduce
to get that output:
var data = [{ maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives Accessories" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Super Abrasives" }]
const r = data.reduce((r,c) => (r.subcatname = [...Array.isArray(r.subcatname)
? r.subcatname
: [r.subcatname], c.subcatname], r))
console.log(r)
The idea is since you only need the subcatname
to be combined you can just use the Array.reduce
and its basic form where it essentially acts as a merge
.
var data = [{ maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives Accessories" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Super Abrasives" }]
const r = data.reduce((r,c) => (r.subcatname = [...Array.isArray(r.subcatname)
? r.subcatname
: [r.subcatname], c.subcatname], r))
console.log(r)
var data = [{ maincategory: "AP", maincatname: "Abrasives", subcatname: "Abrasives Accessories" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Non-woven Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Bonded Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Wire Brushes" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Coated Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Specialty Abrasives" }, { maincategory: "AP", maincatname: "Abrasives", subcatname: "Super Abrasives" }]
const r = data.reduce((r,c) => (r.subcatname = [...Array.isArray(r.subcatname)
? r.subcatname
: [r.subcatname], c.subcatname], r))
console.log(r)
edited Nov 21 '18 at 19:14
answered Nov 21 '18 at 17:53
Akrion
9,40711224
9,40711224
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416917%2fhow-to-merge-objects-thats-inside-an-array-using-native-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown