skip to Main Content

I like to access/extract specific key values of "serviceCode" the array, how could i do that.

    var x = {


    "0": "{"carrierCode":"ups","serviceCode":"ups_ground","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"28.66","baseAmount":"20.19","surcharges":[{"description":"Fuel Surcharge Ground","amount":"3.28"},{"description":"Residential Ground","amount":"2.01"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"8","quotedWeight":"25","quotedWeightType":"Actual"}",
        "1": "{"carrierCode":"ups","serviceCode":"ups_second_day_air","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"69.72","baseAmount":"54.94","surcharges":[{"description":"Fuel Surcharge - Domestic Air","amount":"9.55"},{"description":"Residential Express","amount":"2.05"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"208","quotedWeight":"25","quotedWeightType":"Actual"}",
        "2": "{"carrierCode":"ups","serviceCode":"ups_next_day_air","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"76.62","baseAmount":"60.85","surcharges":[{"description":"Fuel Surcharge - Domestic Air","amount":"10.54"},{"description":"Residential Express","amount":"2.05"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"108","quotedWeight":"25","quotedWeightType":"Actual"}"
    };

const me = Object.create(x);

var arr = [];
$.each( me, function( index, value){
    arr.push(value);
    
});

console.log(arr.carrierCode);

It always return undefined

3

Answers


  1. The values in your array are JSON strings and must be first decoded to objects. Then you can retrieve the serviceCode values from them:

    var x = {
    
    
        "0": "{"carrierCode":"ups","serviceCode":"ups_ground","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"28.66","baseAmount":"20.19","surcharges":[{"description":"Fuel Surcharge Ground","amount":"3.28"},{"description":"Residential Ground","amount":"2.01"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"8","quotedWeight":"25","quotedWeightType":"Actual"}",
            "1": "{"carrierCode":"ups","serviceCode":"ups_second_day_air","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"69.72","baseAmount":"54.94","surcharges":[{"description":"Fuel Surcharge - Domestic Air","amount":"9.55"},{"description":"Residential Express","amount":"2.05"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"208","quotedWeight":"25","quotedWeightType":"Actual"}",
            "2": "{"carrierCode":"ups","serviceCode":"ups_next_day_air","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"76.62","baseAmount":"60.85","surcharges":[{"description":"Fuel Surcharge - Domestic Air","amount":"10.54"},{"description":"Residential Express","amount":"2.05"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"108","quotedWeight":"25","quotedWeightType":"Actual"}"
        };
    
    const me = Object.create(x);
    
    var arr = [];
    $.each( me, function( index, value){
        let obj = JSON.parse(value); // Decode JSON into an object
        arr.push(obj.serviceCode); // Retrieve the value from the object        
    });
    
    console.log(arr);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    Login or Signup to reply.
  2. Below javascript code will work without jQuery

    function getServiceCodes(input) {
      const serviceCodes = [];
      for (const key in input) {
        const option = JSON.parse(input[key]);
        serviceCodes.push(option.serviceCode);
      }
      return serviceCodes;
    }
    
    const input = {
      "0": "{"carrierCode":"ups","serviceCode":"ups_ground","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"28.66","baseAmount":"20.19","surcharges":[{"description":"Fuel Surcharge Ground","amount":"3.28"},{"description":"Residential Ground","amount":"2.01"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"8","quotedWeight":"25","quotedWeightType":"Actual"}",
      "1": "{"carrierCode":"ups","serviceCode":"ups_second_day_air","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"69.72","baseAmount":"54.94","surcharges":[{"description":"Fuel Surcharge - Domestic Air","amount":"9.55"},{"description":"Residential Express","amount":"2.05"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"208","quotedWeight":"25","quotedWeightType":"Actual"}",
      "2": "{"carrierCode":"ups","serviceCode":"ups_next_day_air","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"76.62","baseAmount":"60.85","surcharges":[{"description":"Fuel Surcharge - Domestic Air","amount":"10.54"},{"description":"Residential Express","amount":"2.05"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"108","quotedWeight":"25","quotedWeightType":"Actual"}"
    };
    
    const output = getServiceCodes(input);
    console.log(output);
    Login or Signup to reply.
  3. You don’t need jQuery anymore to manipulate data, keep it for DOM.

    Just map the object values with parsing them with JSON.parse.
    If the JSON is returned from the backend, I would ask the backender to provide it parsed.

    If you need your data later I would suggest to mutate it into an array and parse it for later use including your case.

    Btw stop using var – it’s legacy and creates many subtle bugs.

    let x = {
      "0": "{"carrierCode":"ups","serviceCode":"ups_ground","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"28.66","baseAmount":"20.19","surcharges":[{"description":"Fuel Surcharge Ground","amount":"3.28"},{"description":"Residential Ground","amount":"2.01"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"8","quotedWeight":"25","quotedWeightType":"Actual"}",
      "1": "{"carrierCode":"ups","serviceCode":"ups_second_day_air","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"69.72","baseAmount":"54.94","surcharges":[{"description":"Fuel Surcharge - Domestic Air","amount":"9.55"},{"description":"Residential Express","amount":"2.05"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"208","quotedWeight":"25","quotedWeightType":"Actual"}",
      "2": "{"carrierCode":"ups","serviceCode":"ups_next_day_air","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"76.62","baseAmount":"60.85","surcharges":[{"description":"Fuel Surcharge - Domestic Air","amount":"10.54"},{"description":"Residential Express","amount":"2.05"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"108","quotedWeight":"25","quotedWeightType":"Actual"}"
    };
    
    x = Object.values(x).map(JSON.parse);
    
    const result = x.map(e => e.serviceCode);
    
    console.log(result);

    But if you need the serviceCode property only:

    const input = {
      "0": "{"carrierCode":"ups","serviceCode":"ups_ground","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"28.66","baseAmount":"20.19","surcharges":[{"description":"Fuel Surcharge Ground","amount":"3.28"},{"description":"Residential Ground","amount":"2.01"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"8","quotedWeight":"25","quotedWeightType":"Actual"}",
      "1": "{"carrierCode":"ups","serviceCode":"ups_second_day_air","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"69.72","baseAmount":"54.94","surcharges":[{"description":"Fuel Surcharge - Domestic Air","amount":"9.55"},{"description":"Residential Express","amount":"2.05"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"208","quotedWeight":"25","quotedWeightType":"Actual"}",
      "2": "{"carrierCode":"ups","serviceCode":"ups_next_day_air","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"76.62","baseAmount":"60.85","surcharges":[{"description":"Fuel Surcharge - Domestic Air","amount":"10.54"},{"description":"Residential Express","amount":"2.05"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"108","quotedWeight":"25","quotedWeightType":"Actual"}"
    };
    
    const result = Object.values(input).map(e => JSON.parse(e).serviceCode);
    
    console.log(result);

    Another option is parse with a regex, it’s faster since you don’t need to parse the whole JSON (if you don’t need the rest of data later)

    const input = {
      "0": "{"carrierCode":"ups","serviceCode":"ups_ground","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"28.66","baseAmount":"20.19","surcharges":[{"description":"Fuel Surcharge Ground","amount":"3.28"},{"description":"Residential Ground","amount":"2.01"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"8","quotedWeight":"25","quotedWeightType":"Actual"}",
      "1": "{"carrierCode":"ups","serviceCode":"ups_second_day_air","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"69.72","baseAmount":"54.94","surcharges":[{"description":"Fuel Surcharge - Domestic Air","amount":"9.55"},{"description":"Residential Express","amount":"2.05"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"208","quotedWeight":"25","quotedWeightType":"Actual"}",
      "2": "{"carrierCode":"ups","serviceCode":"ups_next_day_air","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"76.62","baseAmount":"60.85","surcharges":[{"description":"Fuel Surcharge - Domestic Air","amount":"10.54"},{"description":"Residential Express","amount":"2.05"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"108","quotedWeight":"25","quotedWeightType":"Actual"}"
    };
    
    const result = Object.values(input).map(e => e.match(/(?<=serviceCode":")[^"]+/)[0]);
    
    console.log(result);
    ` Chrome/120
    ----------------------------------------------------------------------
    regex - Alexander          1.00x  |  x1000000  465  467  490  497  507
    JSON.parse - Alexander     5.05x  |   x100000  235  239  245  246  252
    jQuery - hotmarycorleone   5.55x  |   x100000  258  267  269  270  273
    ----------------------------------------------------------------------
    https://github.com/silentmantra/benchmark `
    
    const input = {
      "0": "{"carrierCode":"ups","serviceCode":"ups_ground","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"28.66","baseAmount":"20.19","surcharges":[{"description":"Fuel Surcharge Ground","amount":"3.28"},{"description":"Residential Ground","amount":"2.01"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"8","quotedWeight":"25","quotedWeightType":"Actual"}",
      "1": "{"carrierCode":"ups","serviceCode":"ups_second_day_air","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"69.72","baseAmount":"54.94","surcharges":[{"description":"Fuel Surcharge - Domestic Air","amount":"9.55"},{"description":"Residential Express","amount":"2.05"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"208","quotedWeight":"25","quotedWeightType":"Actual"}",
      "2": "{"carrierCode":"ups","serviceCode":"ups_next_day_air","packageTypeCode":"ups_custom_package","currency":"USD","totalAmount":"76.62","baseAmount":"60.85","surcharges":[{"description":"Fuel Surcharge - Domestic Air","amount":"10.54"},{"description":"Residential Express","amount":"2.05"},{"description":"Delivery Confirmation Signature","amount":"3.18"}],"zone":"108","quotedWeight":"25","quotedWeightType":"Actual"}"
    };
    
    // @benchmark jQuery - hotmarycorleone
    var arr = [];
    $.each( input, function( index, value){
        let obj = JSON.parse(value); // Decode JSON into an object
        arr.push(obj.serviceCode); // Retrieve the value from the object        
    });
    arr;
    
    
    // @benchmark JSON.parse - Alexander
    Object.values(input).map(e => JSON.parse(e).serviceCode);
    
    // @benchmark regex - Alexander
    Object.values(input).map(e => e.match(/(?<=serviceCode":")[^"]+/)[0]);
    
    /*@end*/eval(atob('e2xldCBlPWRvY3VtZW50LmJvZHkucXVlcnlTZWxlY3Rvcigic2NyaXB0Iik7aWYoIWUubWF0Y2hlcygiW2JlbmNobWFya10iKSl7bGV0IHQ9ZG9jdW1lbnQuY3JlYXRlRWxlbWVudCgic2NyaXB0Iik7dC5zcmM9Imh0dHBzOi8vY2RuLmpzZGVsaXZyLm5ldC9naC9zaWxlbnRtYW50cmEvYmVuY2htYXJrL2xvYWRlci5qcyIsdC5kZWZlcj0hMCxkb2N1bWVudC5oZWFkLmFwcGVuZENoaWxkKHQpfX0='));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search