Javascript : Store JSON data in array












1















I have JSON like this.



[{
"address": "A-D-1",
"batch": [{
"batch_number": "B-123",
"cost": [{
"cost": "C1"
}]
}]
},
{
"address": "A-85-1",
"batch": [{
"batch_number": "B-6562",
"cost": [{
"cost": "C16464"
}]
}]
},
{
"address": "A-522-1",
"batch": [{
"batch_number": "B-4511",
"cost": [{
"cost": "C8745"
}]
}]
}]


I would like to store my JSON data to array.






let data = JSON.parse('[{"address":"A-D-1","batch":[{"batch_number":"B-123","cost":[{"cost":"C1"}]}]},{"address":"A-85-1","batch":[{"batch_number":"B-6562","cost":[{"cost":"C16464"}]}]},{"address":"A-522-1","batch":[{"batch_number":"B-4511","cost":[{"cost":"C8745"}]}]}]');

for (let i = 0; i < data.length; i++) {

if (data[i].batch !== undefined && data[i].batch !== null && data[i].batch.length !== undefined && data[i].batch.length > 0) {
let batchLength = data[i].batch.length

let newObject = {}
let newArray =

for (let j = 0; j < batchLength; j++) {
if (data[i].batch !== undefined && data[i].batch[j].cost !== null && data[i].batch[j].cost.length !== undefined && data[i].batch[j].cost.length > 0) {
let costLength = data[i].batch[j].cost.length
for (let k = 0; k < costLength; k++) {

newObject.location = data[i].address
newObject.batch.number = data[i].batch[j].batch_number ? data[i].batch[j].batch_number : ''

newObject.cogs = data[i].batch[j].cost[k].cost ? data[i].batch[j].cost[k].cost : ''
newArray.push(newObject)
}
}
}
}

}





I have stored JSON in data variable.



I have tried below code. but I always get last index as repeatative.



Expected Output :



[
{
"address":"A-D-1",
"batch":{
"batch_number":"B-123"
},
"cost":"C1"
},
{
"address":"A-85-1",
"batch":{
"batch_number":"B-6562"
},
"cost":"C16464"
},
{
"address":"A-522-1",
"batch":{
"batch_number":"B-4511"
},
"cost":"C8745"
}
]


Any help would be great.



Thank You.










share|improve this question

























  • Do you receive the JSON data as String?

    – Mis94
    Nov 19 '18 at 17:30











  • I get as an array of objects.

    – Brijesh Patel
    Nov 19 '18 at 17:31











  • How should the result look like?

    – tevemadar
    Nov 19 '18 at 17:32











  • @tevemadar. I have edited my question. Can you please check ?

    – Brijesh Patel
    Nov 19 '18 at 17:38
















1















I have JSON like this.



[{
"address": "A-D-1",
"batch": [{
"batch_number": "B-123",
"cost": [{
"cost": "C1"
}]
}]
},
{
"address": "A-85-1",
"batch": [{
"batch_number": "B-6562",
"cost": [{
"cost": "C16464"
}]
}]
},
{
"address": "A-522-1",
"batch": [{
"batch_number": "B-4511",
"cost": [{
"cost": "C8745"
}]
}]
}]


I would like to store my JSON data to array.






let data = JSON.parse('[{"address":"A-D-1","batch":[{"batch_number":"B-123","cost":[{"cost":"C1"}]}]},{"address":"A-85-1","batch":[{"batch_number":"B-6562","cost":[{"cost":"C16464"}]}]},{"address":"A-522-1","batch":[{"batch_number":"B-4511","cost":[{"cost":"C8745"}]}]}]');

for (let i = 0; i < data.length; i++) {

if (data[i].batch !== undefined && data[i].batch !== null && data[i].batch.length !== undefined && data[i].batch.length > 0) {
let batchLength = data[i].batch.length

let newObject = {}
let newArray =

for (let j = 0; j < batchLength; j++) {
if (data[i].batch !== undefined && data[i].batch[j].cost !== null && data[i].batch[j].cost.length !== undefined && data[i].batch[j].cost.length > 0) {
let costLength = data[i].batch[j].cost.length
for (let k = 0; k < costLength; k++) {

newObject.location = data[i].address
newObject.batch.number = data[i].batch[j].batch_number ? data[i].batch[j].batch_number : ''

newObject.cogs = data[i].batch[j].cost[k].cost ? data[i].batch[j].cost[k].cost : ''
newArray.push(newObject)
}
}
}
}

}





I have stored JSON in data variable.



I have tried below code. but I always get last index as repeatative.



Expected Output :



[
{
"address":"A-D-1",
"batch":{
"batch_number":"B-123"
},
"cost":"C1"
},
{
"address":"A-85-1",
"batch":{
"batch_number":"B-6562"
},
"cost":"C16464"
},
{
"address":"A-522-1",
"batch":{
"batch_number":"B-4511"
},
"cost":"C8745"
}
]


Any help would be great.



Thank You.










share|improve this question

























  • Do you receive the JSON data as String?

    – Mis94
    Nov 19 '18 at 17:30











  • I get as an array of objects.

    – Brijesh Patel
    Nov 19 '18 at 17:31











  • How should the result look like?

    – tevemadar
    Nov 19 '18 at 17:32











  • @tevemadar. I have edited my question. Can you please check ?

    – Brijesh Patel
    Nov 19 '18 at 17:38














1












1








1


1






I have JSON like this.



[{
"address": "A-D-1",
"batch": [{
"batch_number": "B-123",
"cost": [{
"cost": "C1"
}]
}]
},
{
"address": "A-85-1",
"batch": [{
"batch_number": "B-6562",
"cost": [{
"cost": "C16464"
}]
}]
},
{
"address": "A-522-1",
"batch": [{
"batch_number": "B-4511",
"cost": [{
"cost": "C8745"
}]
}]
}]


I would like to store my JSON data to array.






let data = JSON.parse('[{"address":"A-D-1","batch":[{"batch_number":"B-123","cost":[{"cost":"C1"}]}]},{"address":"A-85-1","batch":[{"batch_number":"B-6562","cost":[{"cost":"C16464"}]}]},{"address":"A-522-1","batch":[{"batch_number":"B-4511","cost":[{"cost":"C8745"}]}]}]');

for (let i = 0; i < data.length; i++) {

if (data[i].batch !== undefined && data[i].batch !== null && data[i].batch.length !== undefined && data[i].batch.length > 0) {
let batchLength = data[i].batch.length

let newObject = {}
let newArray =

for (let j = 0; j < batchLength; j++) {
if (data[i].batch !== undefined && data[i].batch[j].cost !== null && data[i].batch[j].cost.length !== undefined && data[i].batch[j].cost.length > 0) {
let costLength = data[i].batch[j].cost.length
for (let k = 0; k < costLength; k++) {

newObject.location = data[i].address
newObject.batch.number = data[i].batch[j].batch_number ? data[i].batch[j].batch_number : ''

newObject.cogs = data[i].batch[j].cost[k].cost ? data[i].batch[j].cost[k].cost : ''
newArray.push(newObject)
}
}
}
}

}





I have stored JSON in data variable.



I have tried below code. but I always get last index as repeatative.



Expected Output :



[
{
"address":"A-D-1",
"batch":{
"batch_number":"B-123"
},
"cost":"C1"
},
{
"address":"A-85-1",
"batch":{
"batch_number":"B-6562"
},
"cost":"C16464"
},
{
"address":"A-522-1",
"batch":{
"batch_number":"B-4511"
},
"cost":"C8745"
}
]


Any help would be great.



Thank You.










share|improve this question
















I have JSON like this.



[{
"address": "A-D-1",
"batch": [{
"batch_number": "B-123",
"cost": [{
"cost": "C1"
}]
}]
},
{
"address": "A-85-1",
"batch": [{
"batch_number": "B-6562",
"cost": [{
"cost": "C16464"
}]
}]
},
{
"address": "A-522-1",
"batch": [{
"batch_number": "B-4511",
"cost": [{
"cost": "C8745"
}]
}]
}]


I would like to store my JSON data to array.






let data = JSON.parse('[{"address":"A-D-1","batch":[{"batch_number":"B-123","cost":[{"cost":"C1"}]}]},{"address":"A-85-1","batch":[{"batch_number":"B-6562","cost":[{"cost":"C16464"}]}]},{"address":"A-522-1","batch":[{"batch_number":"B-4511","cost":[{"cost":"C8745"}]}]}]');

for (let i = 0; i < data.length; i++) {

if (data[i].batch !== undefined && data[i].batch !== null && data[i].batch.length !== undefined && data[i].batch.length > 0) {
let batchLength = data[i].batch.length

let newObject = {}
let newArray =

for (let j = 0; j < batchLength; j++) {
if (data[i].batch !== undefined && data[i].batch[j].cost !== null && data[i].batch[j].cost.length !== undefined && data[i].batch[j].cost.length > 0) {
let costLength = data[i].batch[j].cost.length
for (let k = 0; k < costLength; k++) {

newObject.location = data[i].address
newObject.batch.number = data[i].batch[j].batch_number ? data[i].batch[j].batch_number : ''

newObject.cogs = data[i].batch[j].cost[k].cost ? data[i].batch[j].cost[k].cost : ''
newArray.push(newObject)
}
}
}
}

}





I have stored JSON in data variable.



I have tried below code. but I always get last index as repeatative.



Expected Output :



[
{
"address":"A-D-1",
"batch":{
"batch_number":"B-123"
},
"cost":"C1"
},
{
"address":"A-85-1",
"batch":{
"batch_number":"B-6562"
},
"cost":"C16464"
},
{
"address":"A-522-1",
"batch":{
"batch_number":"B-4511"
},
"cost":"C8745"
}
]


Any help would be great.



Thank You.






let data = JSON.parse('[{"address":"A-D-1","batch":[{"batch_number":"B-123","cost":[{"cost":"C1"}]}]},{"address":"A-85-1","batch":[{"batch_number":"B-6562","cost":[{"cost":"C16464"}]}]},{"address":"A-522-1","batch":[{"batch_number":"B-4511","cost":[{"cost":"C8745"}]}]}]');

for (let i = 0; i < data.length; i++) {

if (data[i].batch !== undefined && data[i].batch !== null && data[i].batch.length !== undefined && data[i].batch.length > 0) {
let batchLength = data[i].batch.length

let newObject = {}
let newArray =

for (let j = 0; j < batchLength; j++) {
if (data[i].batch !== undefined && data[i].batch[j].cost !== null && data[i].batch[j].cost.length !== undefined && data[i].batch[j].cost.length > 0) {
let costLength = data[i].batch[j].cost.length
for (let k = 0; k < costLength; k++) {

newObject.location = data[i].address
newObject.batch.number = data[i].batch[j].batch_number ? data[i].batch[j].batch_number : ''

newObject.cogs = data[i].batch[j].cost[k].cost ? data[i].batch[j].cost[k].cost : ''
newArray.push(newObject)
}
}
}
}

}





let data = JSON.parse('[{"address":"A-D-1","batch":[{"batch_number":"B-123","cost":[{"cost":"C1"}]}]},{"address":"A-85-1","batch":[{"batch_number":"B-6562","cost":[{"cost":"C16464"}]}]},{"address":"A-522-1","batch":[{"batch_number":"B-4511","cost":[{"cost":"C8745"}]}]}]');

for (let i = 0; i < data.length; i++) {

if (data[i].batch !== undefined && data[i].batch !== null && data[i].batch.length !== undefined && data[i].batch.length > 0) {
let batchLength = data[i].batch.length

let newObject = {}
let newArray =

for (let j = 0; j < batchLength; j++) {
if (data[i].batch !== undefined && data[i].batch[j].cost !== null && data[i].batch[j].cost.length !== undefined && data[i].batch[j].cost.length > 0) {
let costLength = data[i].batch[j].cost.length
for (let k = 0; k < costLength; k++) {

newObject.location = data[i].address
newObject.batch.number = data[i].batch[j].batch_number ? data[i].batch[j].batch_number : ''

newObject.cogs = data[i].batch[j].cost[k].cost ? data[i].batch[j].cost[k].cost : ''
newArray.push(newObject)
}
}
}
}

}






javascript json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 17:58









tevemadar

4,3482725




4,3482725










asked Nov 19 '18 at 17:11









Brijesh PatelBrijesh Patel

377




377













  • Do you receive the JSON data as String?

    – Mis94
    Nov 19 '18 at 17:30











  • I get as an array of objects.

    – Brijesh Patel
    Nov 19 '18 at 17:31











  • How should the result look like?

    – tevemadar
    Nov 19 '18 at 17:32











  • @tevemadar. I have edited my question. Can you please check ?

    – Brijesh Patel
    Nov 19 '18 at 17:38



















  • Do you receive the JSON data as String?

    – Mis94
    Nov 19 '18 at 17:30











  • I get as an array of objects.

    – Brijesh Patel
    Nov 19 '18 at 17:31











  • How should the result look like?

    – tevemadar
    Nov 19 '18 at 17:32











  • @tevemadar. I have edited my question. Can you please check ?

    – Brijesh Patel
    Nov 19 '18 at 17:38

















Do you receive the JSON data as String?

– Mis94
Nov 19 '18 at 17:30





Do you receive the JSON data as String?

– Mis94
Nov 19 '18 at 17:30













I get as an array of objects.

– Brijesh Patel
Nov 19 '18 at 17:31





I get as an array of objects.

– Brijesh Patel
Nov 19 '18 at 17:31













How should the result look like?

– tevemadar
Nov 19 '18 at 17:32





How should the result look like?

– tevemadar
Nov 19 '18 at 17:32













@tevemadar. I have edited my question. Can you please check ?

– Brijesh Patel
Nov 19 '18 at 17:38





@tevemadar. I have edited my question. Can you please check ?

– Brijesh Patel
Nov 19 '18 at 17:38












2 Answers
2






active

oldest

votes


















0














You mean something like this?






const someArray = [{
"address": "A-D-1",
"batch": [{
"batch_number": "B-123",
"cost": [{
"cost": "C1"
}]
}]
}, {
"address": "A-85-1",
"batch": [{
"batch_number": "B-6562",
"cost": [{
"cost": "C16464"
}]
}]
}, {
"address": "A-522-1",
"batch": [{
"batch_number": "B-4511",
"cost": [{
"cost": "C8745"
}]
}]
}]

console.log(someArray.map(item => {
return {
address:item.address,
batch_number: item.batch && item.batch[0].batch_number,
cost: item.batch && item.batch[0].cost[0].cost
}
}))








share|improve this answer
























  • yes but I can have multiple arrays inside each array. so how can I solve that issue ?

    – Brijesh Patel
    Nov 19 '18 at 17:29



















0














You can use below method to retrieve the same in array format.



const data = JSON.parse(someArray);
console.log (data[0].address);





share|improve this answer























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


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379594%2fjavascript-store-json-data-in-array%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You mean something like this?






    const someArray = [{
    "address": "A-D-1",
    "batch": [{
    "batch_number": "B-123",
    "cost": [{
    "cost": "C1"
    }]
    }]
    }, {
    "address": "A-85-1",
    "batch": [{
    "batch_number": "B-6562",
    "cost": [{
    "cost": "C16464"
    }]
    }]
    }, {
    "address": "A-522-1",
    "batch": [{
    "batch_number": "B-4511",
    "cost": [{
    "cost": "C8745"
    }]
    }]
    }]

    console.log(someArray.map(item => {
    return {
    address:item.address,
    batch_number: item.batch && item.batch[0].batch_number,
    cost: item.batch && item.batch[0].cost[0].cost
    }
    }))








    share|improve this answer
























    • yes but I can have multiple arrays inside each array. so how can I solve that issue ?

      – Brijesh Patel
      Nov 19 '18 at 17:29
















    0














    You mean something like this?






    const someArray = [{
    "address": "A-D-1",
    "batch": [{
    "batch_number": "B-123",
    "cost": [{
    "cost": "C1"
    }]
    }]
    }, {
    "address": "A-85-1",
    "batch": [{
    "batch_number": "B-6562",
    "cost": [{
    "cost": "C16464"
    }]
    }]
    }, {
    "address": "A-522-1",
    "batch": [{
    "batch_number": "B-4511",
    "cost": [{
    "cost": "C8745"
    }]
    }]
    }]

    console.log(someArray.map(item => {
    return {
    address:item.address,
    batch_number: item.batch && item.batch[0].batch_number,
    cost: item.batch && item.batch[0].cost[0].cost
    }
    }))








    share|improve this answer
























    • yes but I can have multiple arrays inside each array. so how can I solve that issue ?

      – Brijesh Patel
      Nov 19 '18 at 17:29














    0












    0








    0







    You mean something like this?






    const someArray = [{
    "address": "A-D-1",
    "batch": [{
    "batch_number": "B-123",
    "cost": [{
    "cost": "C1"
    }]
    }]
    }, {
    "address": "A-85-1",
    "batch": [{
    "batch_number": "B-6562",
    "cost": [{
    "cost": "C16464"
    }]
    }]
    }, {
    "address": "A-522-1",
    "batch": [{
    "batch_number": "B-4511",
    "cost": [{
    "cost": "C8745"
    }]
    }]
    }]

    console.log(someArray.map(item => {
    return {
    address:item.address,
    batch_number: item.batch && item.batch[0].batch_number,
    cost: item.batch && item.batch[0].cost[0].cost
    }
    }))








    share|improve this answer













    You mean something like this?






    const someArray = [{
    "address": "A-D-1",
    "batch": [{
    "batch_number": "B-123",
    "cost": [{
    "cost": "C1"
    }]
    }]
    }, {
    "address": "A-85-1",
    "batch": [{
    "batch_number": "B-6562",
    "cost": [{
    "cost": "C16464"
    }]
    }]
    }, {
    "address": "A-522-1",
    "batch": [{
    "batch_number": "B-4511",
    "cost": [{
    "cost": "C8745"
    }]
    }]
    }]

    console.log(someArray.map(item => {
    return {
    address:item.address,
    batch_number: item.batch && item.batch[0].batch_number,
    cost: item.batch && item.batch[0].cost[0].cost
    }
    }))








    const someArray = [{
    "address": "A-D-1",
    "batch": [{
    "batch_number": "B-123",
    "cost": [{
    "cost": "C1"
    }]
    }]
    }, {
    "address": "A-85-1",
    "batch": [{
    "batch_number": "B-6562",
    "cost": [{
    "cost": "C16464"
    }]
    }]
    }, {
    "address": "A-522-1",
    "batch": [{
    "batch_number": "B-4511",
    "cost": [{
    "cost": "C8745"
    }]
    }]
    }]

    console.log(someArray.map(item => {
    return {
    address:item.address,
    batch_number: item.batch && item.batch[0].batch_number,
    cost: item.batch && item.batch[0].cost[0].cost
    }
    }))





    const someArray = [{
    "address": "A-D-1",
    "batch": [{
    "batch_number": "B-123",
    "cost": [{
    "cost": "C1"
    }]
    }]
    }, {
    "address": "A-85-1",
    "batch": [{
    "batch_number": "B-6562",
    "cost": [{
    "cost": "C16464"
    }]
    }]
    }, {
    "address": "A-522-1",
    "batch": [{
    "batch_number": "B-4511",
    "cost": [{
    "cost": "C8745"
    }]
    }]
    }]

    console.log(someArray.map(item => {
    return {
    address:item.address,
    batch_number: item.batch && item.batch[0].batch_number,
    cost: item.batch && item.batch[0].cost[0].cost
    }
    }))






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 '18 at 17:28









    Arulmozhi ManikandanArulmozhi Manikandan

    1766




    1766













    • yes but I can have multiple arrays inside each array. so how can I solve that issue ?

      – Brijesh Patel
      Nov 19 '18 at 17:29



















    • yes but I can have multiple arrays inside each array. so how can I solve that issue ?

      – Brijesh Patel
      Nov 19 '18 at 17:29

















    yes but I can have multiple arrays inside each array. so how can I solve that issue ?

    – Brijesh Patel
    Nov 19 '18 at 17:29





    yes but I can have multiple arrays inside each array. so how can I solve that issue ?

    – Brijesh Patel
    Nov 19 '18 at 17:29













    0














    You can use below method to retrieve the same in array format.



    const data = JSON.parse(someArray);
    console.log (data[0].address);





    share|improve this answer




























      0














      You can use below method to retrieve the same in array format.



      const data = JSON.parse(someArray);
      console.log (data[0].address);





      share|improve this answer


























        0












        0








        0







        You can use below method to retrieve the same in array format.



        const data = JSON.parse(someArray);
        console.log (data[0].address);





        share|improve this answer













        You can use below method to retrieve the same in array format.



        const data = JSON.parse(someArray);
        console.log (data[0].address);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 17:40









        MuraliMurali

        34




        34






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379594%2fjavascript-store-json-data-in-array%23new-answer', 'question_page');
            }
            );

            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







            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word