3

I have a json array in the below format:

[
  {
     "id":"01",
     "language": "Java",
     "edition": "third",
     "author": "Herbert Schildt"
  },
  {
     "id":"07",
     "language": "C++",
     "edition": "second"
     "author": "E.Balagurusamy"
  }
]

I want a result consisting only the first column from this array in the form:

[ "01", "07" ]

I tried many codes but wouldn't succeed, is there a way to achieve this using jquery.

2 Answers 2

5

You can do it like following using map() function.

var input = [
    {
        "id": "01",
        "language": "Java",
        "edition": "third",
        "author": "Herbert Schildt"
    },
    {
        "id": "07",
        "language": "C++",
        "edition": "second",
        "author": "E.Balagurusamy"
    }
];

var result = $(input).map(function() {
    return this.id;
}).get()

console.log(result);
Sign up to request clarification or add additional context in comments.

Comments

1

For anyone looking for the same thing in plain javascript (adapted from acepted answer)...

var input = [
    {
        "id": "01",
        "language": "Java",
        "edition": "third",
        "author": "Herbert Schildt"
    },
    {
        "id": "07",
        "language": "C++",
        "edition": "second",
        "author": "E.Balagurusamy"
    }
];

var result = input.map(function(item)
{
    return item.id;
});

console.log(result);

Or in fancy ES6...

var result = input.map(item => item.id);

Comments

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.