0

I want to create an array of object shown in below code.

myobj = [{
 name: "abc",
 "code": "aa",
 "isdefault": 1
}, {
 name: "abc",
 "code": "bb",
 "isdefault": 0
}, {
 name: "bcd",
 "code": "ac",
 "isdefault": 0
}]
var codeids = {};
$.each(myobj, function(key, val) {
 console.log(val); // ==>{name: "abc","code":4,"isdefault":1 }
 codeids[val.name] = {
 [val.code]: val.isdefault
 }; // I want codeids['abc'] should be ==> [{"aa":1},{"ab":0}...]
});
console.log(codeids)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

final codeids should be ==>

{"abc":[{"aa":1},{"ab":0}],"bcd":[{"ac":0},{"cd":0}],...}*

how can I achieve this?

mplungjan
180k29 gold badges183 silver badges246 bronze badges
asked Sep 8, 2020 at 7:43

5 Answers 5

1

You can do it also using reduce method.

let myobj = [{
 name: "abc",
 "code": "aa",
 "isdefault": 1
}, {
 name: "abc",
 "code": "bb",
 "isdefault": 0
}, {
 name: "bcd",
 "code": "ac",
 "isdefault": 0
}]
let y = myobj.reduce((acc, obj) => {
 if (!acc[obj.name]) {
 acc[obj.name] = []
 }
 acc[obj.name].push({
 [obj.code]: obj.isdefault
 })
 return acc
}, {})
console.log(y)

answered Sep 8, 2020 at 8:01
Sign up to request clarification or add additional context in comments.

Comments

0

You need to push onto an array, not just assign directly to the property.

myobj = [{
 name: "abc",
 "code": "aa",
 "isdefault": 1
}, {
 name: "abc",
 "code": "bb",
 "isdefault": 0
}, {
 name: "bcd",
 "code": "ac",
 "isdefault": 0
}]
var codeids = {};
$.each(myobj, function(key, val) {
 if (!codeids[val.name]) {
 // initialize empty array
 codeids[val.name] = [];
 }
 codeids[val.name].push({
 [val.code]: val.isdefault
 });
});
console.log(codeids);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

answered Sep 8, 2020 at 7:49

Comments

0

You could take the values from the object and check if the property exists for the array.

This approach works without jquery.

const
 array = [{ name: "abc", code: "aa", isdefault: 1 }, { name: "abc", code: "bb", isdefault: 0 }, { name: "bcd", code: "ac", isdefault:0 }],
 codeids = {};
array.forEach(({ name, code, isdefault }) => {
 if (!codeids[name]) codeids[name] = [];
 codeids[name].push({ [code]: isdefault });
});
console.log(codeids);
.as-console-wrapper { max-height: 100% !important; top: 0; }

answered Sep 8, 2020 at 7:50

Comments

0

You can do this with a plain JS forEach loop, no Jquery needed:

myobj = [{
 name: "abc",
 "code": "aa",
 "isdefault": 1
}, {
 name: "abc",
 "code": "bb",
 "isdefault": 0
}, {
 name: "bcd",
 "code": "ac",
 "isdefault": 0
}]
var codeids = {};
myobj.forEach(({name, code, isdefault}) => {
 if (!codeids[name]) codeids[name] = [];
 codeids[name].push({[code]: isdefault});
})
console.log(codeids)

answered Sep 8, 2020 at 7:53

Comments

0

If I understood correctly this should do it:

myobj = [
 {name: "abc","code":"aa","isdefault":1 },
 {name: "abc","code":"bb","isdefault":0 },
 {name: "bcd","code":"ac","isdefault":0 }
 ];
 
var codeids = {};
myobj.forEach((val) => {
 if(!codeids.hasOwnProperty(val.name)) {
 codeids[val.name] = [];
 }
 const newItem = {};
 newItem[val.code] = val.isdefault;
 
 codeids[val.name].push(newItem);
});
console.log(codeids);

answered Sep 8, 2020 at 7:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.