10

I have following JSON string :

{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}

I want location_id as

3,2

3
  • 2
    Is that JSON string enclosed as an array? i.e. surrounded by []? Commented Oct 14, 2016 at 6:04
  • try to use a library for this propouse, json.org/js.html Commented Oct 14, 2016 at 6:04
  • 2
    @Mitul - I've rolled back your edit because without hearing back from the OP I don't think we can just assume that their input string is different to what they've said it is. (Though obviously as is it is not valid JSON.) Commented Oct 14, 2016 at 6:21

8 Answers 8

27

Simple:

var data = [{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}]
var result = data.map(function(val) {
  return val.location_id;
}).join(',');

console.log(result)

I assume you wanted a string, hence the .join(','), if you want an array simply remove that part.

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

2 Comments

FYI, .join by defaults join values by comma.
@Rajesh Yes, but I added it for clarification.
5

You could add brackets to the string, parse the string (JSON.parse) and map (Array#map) the property and the join (Array#join) the result.

var string = '{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}',
    array = JSON.parse('[' + string + ']'),
    result = array.map(function (a) { return a.location_id; }).join();

console.log(result);

2 Comments

OP had edited the question to add the enclosing square brackets.
@MohitBhardwaj - That edit wasn't by the OP, and the OP hasn't replied to a query about it, so for now I've rolled that edit back.
1

obj=[{"name":"Marine Lines","location_id":3}, {"name":"Ghatkopar","location_id":2}]
var res = [];
for (var x  in obj)
if (obj.hasOwnProperty(x))
    res.push(obj[x].location_id);
console.log(res.join(","));

Comments

1

var json = [{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}];

var locationIds = [];
for(var object in json){
  locationIds.push(json[object].location_id);
}

console.log(locationIds.join(","));

Comments

1

You can also look into .reduce and create a string manually

var d = [{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}]

var location_id_str = d.reduce(function(p, c) {
  return p ? p + ',' + c.location_id : c.location_id
},'');

console.log(location_id_str)

2 Comments

This doesn't work if there are more than two objects in the array.
Perfect Solution. Mine more than 500 values, its took only 100ms time .
0

try this

    var obj = [{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}];
    var output = obj.map( function(item){
      return item.location_id;
    });
    console.log( output.join(",") )

Comments

0

var arr = [{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}];

var location_array = [];
for( var i = 0; i < arr.length; i++ )
{
  location_array.push( arr[i].location_id );
}//for

var location_string = location_array.join(",");
console.log(location_string);

Note: You may need to use JSON.parse() if the arr is in string format initially.

Comments

0

You can use for..of loop

var arr = [{
  "name": "Marine Lines",
  "location_id": 3
}, {
  "name": "Ghatkopar",
  "location_id": 2
}];

var res = [];

for ({location_id} of arr) {res.push(location_id)};

console.log(res);

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.