1

Here's my json::

[{ "name": "Roy", "city": "Dallas", "state": "Texas" }, { "name": "Karla", "city": "LA", "state": "California" },{ "name": "Felix", "city": "Las Vegas", "state": "Nevada" },{ "name": "Fred", "city": "Miami", "state": "Florida" },{ "name": "Bill", "city": "Atlanta", "state": "Georgia" },{ "name": "Mike", "city": "Chicago", "state": "Illinois" },{ "name": "Tim", "city": "Worcester", "state": "MA" },{ "name": "Ryan", "city": "Austin", "state": "Texas" },{ "name": "Sam", "city": "Boston", "state": "MA" },{ "name": "Sarah", "city": "Houston", "state": "Texas" }]

I want to capture all index numbers with state of Texas:

Please help.

2
  • Can you specify what you're struggling with? JSON parsing? Loop logic? A combination of the two? Commented Aug 25, 2017 at 18:29
  • loop logic.I wan't to get all index numbers. Commented Aug 25, 2017 at 18:32

5 Answers 5

1

You could use Array#reduce while collecting indices of matching state.

var array = [{ name: "Roy", city: "Dallas", state: "Texas" }, { name: "Karla", city: "LA", state: "California" }, { name: "Felix", city: "Las Vegas", state: "Nevada" }, { name: "Fred", city: "Miami", state: "Florida" }, { name: "Bill", city: "Atlanta", state: "Georgia" }, { name: "Mike", city: "Chicago", state: "Illinois" }, { name: "Tim", city: "Worcester", state: "MA" }, { name: "Ryan", city: "Austin", state: "Texas" }, { name: "Sam", city: "Boston", state: "MA" }, { name: "Sarah", city: "Houston", state: "Texas" }],
    indices = array.reduce((r, o, i) => (o.state === 'Texas' && r.push(i), r), []);

console.log(indices);

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

Comments

1

You can use map() and filter() methods like following.

var data = [{ "name": "Roy", "city": "Dallas", "state": "Texas" }, { "name": "Karla", "city": "LA", "state": "California" },{ "name": "Felix", "city": "Las Vegas", "state": "Nevada" },{ "name": "Fred", "city": "Miami", "state": "Florida" },{ "name": "Bill", "city": "Atlanta", "state": "Georgia" },{ "name": "Mike", "city": "Chicago", "state": "Illinois" },{ "name": "Tim", "city": "Worcester", "state": "MA" },{ "name": "Ryan", "city": "Austin", "state": "Texas" },{ "name": "Sam", "city": "Boston", "state": "MA" },{ "name": "Sarah", "city": "Houston", "state": "Texas" }];

var indexes = data.map(function(item, i){
    if(item.state == "Texas") return i;
}).filter(function(item){ return item!=undefined; });

console.log(indexes);

Comments

0

You can enumerate the collection and add the indexes in an array, where state is 'Texas'. If you are using server side code, you can Deserialize the response (to a target type that contains same attributes as JSON object) using JSON parser. If you are using Javascript, you can JSON.parse(jsonString) to convert into an object array and then enumerate

Comments

0

You can use a for loop to achieve this. Iterating over a variable, say 'i', would be useful.

texas_indecies = [];
for (var i = 0; i < array.length; i++){
  if (array[i].state == "Texas"){
    texas_indecies.push(i);
  }
}

Comments

0

is this what you need?

var obj=[{ "name": "Roy", "city": "Dallas", "state": "Texas" }, { "name": "Karla", "city": "LA", "state": "California" },{ "name": "Felix", "city": "Las Vegas", "state": "Nevada" },{ "name": "Fred", "city": "Miami", "state": "Florida" },{ "name": "Bill", "city": "Atlanta", "state": "Georgia" },{ "name": "Mike", "city": "Chicago", "state": "Illinois" },{ "name": "Tim", "city": "Worcester", "state": "MA" },{ "name": "Ryan", "city": "Austin", "state": "Texas" },{ "name": "Sam", "city": "Boston", "state": "MA" },{ "name": "Sarah", "city": "Houston", "state": "Texas" }],
indexes=[];

for(var index in obj){
  if(obj[index]['state']=='Texas')
    indexes.push(index);
}

console.log(indexes);

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.