0

I was thinking that i know much about javascript but i must have to admit i know nothing. i have been trying to create a custom object but still seems no where.

var currentline=[
        {"NAME":"Battery For Alarm Panel","CODE":"POWER MAX","OWN":"ONM"},
        {"NAME":"Fire Alarm Panel","CODE":"SANA SERVICES","OWN":"ONM"}...
       ]

Through help of following question i'm able to create following object

   // skipping main loop code for brevity
   detailObj=[];
   loop{
     singleObj = {};
     singleObj[currentline['NAME']] = {};
     singleObj[currentline['NAME']][currentline['CODE']] = {};
     singleObj[currentline['NAME']][currentline['CODE']][currentline['OWN']] = value;
     detailObj.push(singleObj);
   }

AND get the following detailObj

       [
        {"Battery For Alarm Panel":{"POWER MAX":{"ONM":7}}},
        {"Fire Alarm Panel":{"SANA SERVICES":{"ONM":8}}}
       ]

how i can push an object into object despite into array and could get following object?

       {
       "Battery For Alarm Panel":{"POWER MAX":{"ONM":7}},
       "Fire Alarm Panel":{"SANA SERVICES":{"ONM":8}}
       }

is there any function to add object in the object?

4
  • In your prototype code you iterate currentline but currentline is an Array which contain Objects. So a numeric loop is necessary and Then you can access it via currentline[index].NAME Commented Mar 22, 2014 at 11:16
  • yes i know; but ijust has simplyfy it. i just to know the way to add object in the object Commented Mar 22, 2014 at 11:18
  • Please post a working example then it's easier to understand your implementation and support you for the changes which are required to match your goal Commented Mar 22, 2014 at 11:21
  • if i would have post a complete example it might have been a complete mess and might not able to get the early answer Commented Mar 22, 2014 at 11:28

1 Answer 1

1

If currentline is like this

var currentline = [{
    "NAME": "Battery For Alarm Panel",
    "CODE": "POWER MAX",
    "OWN": "ONM",
    "VALUE": 7
}, {
    "NAME": "Fire Alarm Panel",
    "CODE": "SANA SERVICES",
    "OWN": "ONM",
    "VALUE": 8
}];

Simply change the detailObj an object and instead of pushing, assign NAMES values as keys, like this

var detailObj = {};    // An object, not an array
currentline.forEach(function(line) {
    detailObj[line.NAME] = {};
    detailObj[line.NAME][line.CODE] = {};
    detailObj[line.NAME][line.CODE][line.OWN] = line.VALUE;
});
console.log(detailObj);
# { 'Battery For Alarm Panel': { 'POWER MAX': { ONM: 7 } },
#   'Fire Alarm Panel': { 'SANA SERVICES': { ONM: 8 } } }
Sign up to request clarification or add additional context in comments.

1 Comment

@MuhammadHaseebKhan Glad to have been of help! Feel free to accept my answer if you feel it was useful to you. :-)

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.