Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

Javascript/Jquery - Create array format based on JSON response

Used below JS code to get JSON response from endpoint URL. Response value returns correctly like below JSON format.

From this JSON format, i need to extract and display in array format for DATALAYER. But, below code getting output of last time instead of all JSON response items to show.

Format required:

newArray [
  {
    codePlan: "SSS0111",
    title: "Title 1"
  },
  {
    codePlan: "",
    title: "Title 2"
  },
  {
    codePlan: "SSS0888,CCC0222,EEE0001,DDD0009",
    title: "Title 3"
  }
]

But it get last item only and displays like this.

newArray [
  {
    codePlan: "SSS0891",
    title: "Title 5"
  }
]

var endpointUrl = '/data/codelisting.json';

$.get(endpointUrl, function (response) {
    $.each(response.data, function (i, item) {
        var dataLayerObject = {};
        var newArray = [
          {
            codePlan: item.codePlan,
            title: item.title
          }
        ];
        dataLayerObject = {
          arrayParent: {
            newArray : [...newArray]
          }
        };
        DTM.setDataLayer(dataLayerObject);
    });
});

{
   "total": 5,
   "data":[
      {
         "title":"Title 1",
         "description":"description 1"
         "codePlan":[
            "SSS0111"
         ]
      },
      {
         "title":"Title 2",
         "description":"description 2"
         "codePlan":[]
      },
      {
         "title":"Title 3",
         "description":"description 3"
         "codePlan":[
            "SSS0888",
            "CCC0222",
            "EEE0001",
            "DDD0009"
         ]
      },
      {
         "title":"Title 4",
         "description":"description 4"
         "codePlan":[
            "SSS0897"
         ]
      },
      {
         "title":"Title 5",
         "description":"description 5"
         "codePlan":[
            "SSS0891"
         ]
      }
   ]
}
question from:https://stackoverflow.com/questions/65947800/javascript-jquery-create-array-format-based-on-json-response

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem with var newArray = [ it updates the newArray value with a new value each time in a loop .. So you need to define it before .each() var newArray = []; and let .each() add a new value to it newArray[i] =

$.get(endpointUrl, function (response) {    
    var newArray = [];       //<<<<<<<<<<< here
    $.each(response.data, function (i, item) {
      var dataLayerObject = {};
      newArray[i] = [        // <<<<<< use newArray[i] here
          {
            codePlan: item.codePlan,
            title: item.title
          }
        ];
       dataLayerObject = {
         arrayParent: {
         newArray : [...newArray]
       }
    };
    DTM.setDataLayer(dataLayerObject);
  });

var response = [
  {
    codePlan: "SSS0111",
    title: "Title 1"
  },
  {
    codePlan: "",
    title: "Title 2"
  },
  {
    codePlan: "SSS0888,CCC0222,EEE0001,DDD0009",
    title: "Title 3"
  }
];

var newArray = []
$.each(response , function(i , item){
  newArray[i] = [{
    codePlan: item.codePlan,
    title: item.title
  }]
});

console.log(newArray);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...