1

I have the following object I created from a json file using jquery $.get. console.log(array);

 [Object, Object, Object, Object, Object, Object, Object, Object]
    0
    :
    Object
    country
    :
    "Cameroon"
    employ_count
    :
    50
    __proto__
    :
    Object
    1
    :
    bject
    7
    :
    Object
    length:8
    __proto__
    :
    Array[0]

I am trying to convert each object to an array of this type ['Cameroon', 58], ['Germany', 58]....

I tried using jquery $.makeArray

var array = $.makeArray(emObj);

but this still returns an object.

3
  • @nnnnnn typing error. I corrected that! Commented Jul 19, 2016 at 2:55
  • Can you please show a sample of the original JSON? (Is it like [{"country":"Cameroon", "employ_count":50}, {"country":"Germany", "employ_count":58}]?) That console logged version is a bit hard to read. Commented Jul 19, 2016 at 2:56
  • yes , that's what the original looks like { "countries": [ { "country": "Cameroon", "employ_count": 50 } , ...] } Commented Jul 19, 2016 at 2:57

2 Answers 2

3

If I understand you correctly, your input is an array of objects, and your desired output is an array of arrays. This is easy with jQuery's $.map() method, but the only reason to use jQuery for this is if you're supporting IE < 9, because all modern browsers support the native Array.prototype.map() method:

var input = {
  "countries": [
    { "country": "Australia", "employ_count": 22 },
    { "country": "Cameroon", "employ_count": 50 },
    { "country": "Germany", "employ_count": 13 }
  ]
};

var output = input.countries.map(function(v) { 
  return [v.country, v.employ_count];
});

var outputWithjQuery = $.map(input.countries, function(v) { 
  return [[v.country, v.employ_count]];
});

console.log(output);
console.log(outputWithjQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Note that the double [[ ... ]] in the jQuery version is not a typo: if you return an array from the $.map() function jQuery will flatten it, and returning an array of one element that is the actual array you want fixes that. (The native .map() method doesn't have this "feature".)

Sign up to request clarification or add additional context in comments.

Comments

1

You can solve this without using any library dependency like jquery.

res.push([elem['country'], Number(elem['employ_count'])]);

var arr = [{
  country: "Cameroon",
  employ_count: "58"
}, {
  country: "Germany ",
  employ_count: "50"
}];

function calc(arr) {
  var i,
    len,
    res = [];
  for (i = 0, len = arr.length; i < len; i += 1) {
    elem = arr[i];
    res.push([elem['country'], Number(elem['employ_count'])]);
  }
  return res;
}
console.log(calc(arr));

4 Comments

@Obasi Is this what you were looking for? Let me know if you need some changes.
Note: the numbers in the OP's input actually are numbers, so no need to call Number().
In the rest of the program if I concatenate another string say "50" + "28" If not Numbered, it will be "5028". Just wanted to make sure that dont happen. Optional again. :D
But...making the value a number doesn't mean it won't be coerced back to a string if you concatenate it together with a string. Number("50") + "28" is "5028". In any case, as I said before the OP's values are already numbers, not strings.

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.