how to iterate json response value from server in javascript

前端 未结 3 656
独厮守ぢ
独厮守ぢ 2021-01-26 11:19

hii i\'m new to javascript i have an web request and its give response as JSON format, the task is that i need to parse the data into array

here is my sample re

3条回答
  •  情书的邮戳
    2021-01-26 11:28

    If I understood you correctly, this should suit your needs.

    We loop through "Employee_Names" objects.

    // ES6 code    
    var allNames = employee["Employee_Names"].map(bar => bar.Name);
    
    // pure javascript
    var allNames = [];
    for (i = 0; i < employee["Employee_Names"].length; i++) {
      var element = employee["Employee_Names"][i];
    
      // We push only name
      allNames.push( element.Name );
    
      // We push full json object
       //allNames.push( element );
    }
    

    Now we assign new json key with our allNames array.

    employee = Object.assign(employee, { "NEW_NAMES" : allNames });
    

    Check working exmaple:

    var employee = {
      "Employee_Names" : [
         {
            "BAR_RATING" : "0",
            "Name" : "anand",
            "Name_RATING" : "0",
            "PATTERN" : "Ln",
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "av",
            "Name_RATING" : "0",
            "PATTERN" : "FiLi",
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "books",
            "Name_RATING" : "0",
            "PATTERN" : "Ln",
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "kanagalu",
            "Name_RATING" : "0",
            "PATTERN" : null,
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "specialty-av",
            "Name_RATING" : "0",
            "PATTERN" : "Fn-Ln",
    
         }
      ],
      "FOUND_Name" : [ ],
      "OTHER_Name" : [
         {
            "BAR_RATING" : "0",
            "Name" : "kindle-cs-support",
            "Name_RATING" : "0",
            "PATTERN" : null,
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "noreply-ops-jobs",
            "Name_RATING" : "0",
            "PATTERN" : null,
    
         }
      ],
      "PERSONAL_Name" : [  ],
      "PROJECTED_Name" : [
         {
            "BAR_RATING" : "0",
            "Name" : "anand.venkatesan",
            "Name_RATING" : "0",
            "PATTERN" : "Fn.Ln",
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "anandv",
            "Name_RATING" : "0",
            "PATTERN" : "FnLi",
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "vanand",
            "Name_RATING" : "0",
            "PATTERN" : "LiFn",
    
         }
      ]
      };
    
    
    // ES6
    //var allNames = employee["Employee_Names"].map(bar => bar.Name);
    
    // pure javascript
    var allNames = [];
    for (i = 0; i < employee["Employee_Names"].length; i++) {
      var element = employee["Employee_Names"][i];
    
      // We push only name
      allNames.push( element.Name );
      
      // We push full json object
       //allNames.push( element );
    }
    
    // Now we assign new json key with our names
    employee = Object.assign(employee, { "NEW_NAMES" : allNames });
    
    // Our new employee json
    console.log(employee);

提交回复
热议问题