最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - Find index of object in javascript by a value - Stack Overflow

programmeradmin4浏览0评论

I have a long list of timezones in json like the following.

[
 {"value": "Pacific/Niue", "name": "(GMT-11:00) Niue"},
 {"value": "Pacific/Pago_Pago", "name": "(GMT-11:00) Pago Pago"},
 {"value": "Pacific/Honolulu", "name": "(GMT-10:00) Hawaii Time"},
 {"value": "Pacific/Rarotonga", "name": "(GMT-10:00) Rarotonga"},
 {"value": "Pacific/Tahiti", "name": "(GMT-10:00) Tahiti"},
 {"value": "Pacific/Marquesas", "name": "(GMT-09:30) Marquesas"},
 {"value": "America/Anchorage", "name": "(GMT-09:00) Alaska Time"},
 {"value": "Pacific/Gambier", "name": "(GMT-09:00) Gambier"},
 {"value": "America/Los_Angeles", "name": "(GMT-08:00) Pacific Time"},
 {"value": "America/Tijuana", "name": "(GMT-08:00) Pacific Time -  Tijuana"},
 {"value": "America/Vancouver", "name": "(GMT-08:00) Pacific Time - Vancouver"},
]

I have user timezone detection set up which returns a timezone string as "America/Los_Angeles"

Using Javascript I want to find the where America/Los_Angeles is in the json object so I can use its "name" to prefill a form field.

I am familiar with indexOf() method, but can't work out how to use it in this situation. Is there a simple way to handle this or should I just foreach through the whole list?

I have a long list of timezones in json like the following.

[
 {"value": "Pacific/Niue", "name": "(GMT-11:00) Niue"},
 {"value": "Pacific/Pago_Pago", "name": "(GMT-11:00) Pago Pago"},
 {"value": "Pacific/Honolulu", "name": "(GMT-10:00) Hawaii Time"},
 {"value": "Pacific/Rarotonga", "name": "(GMT-10:00) Rarotonga"},
 {"value": "Pacific/Tahiti", "name": "(GMT-10:00) Tahiti"},
 {"value": "Pacific/Marquesas", "name": "(GMT-09:30) Marquesas"},
 {"value": "America/Anchorage", "name": "(GMT-09:00) Alaska Time"},
 {"value": "Pacific/Gambier", "name": "(GMT-09:00) Gambier"},
 {"value": "America/Los_Angeles", "name": "(GMT-08:00) Pacific Time"},
 {"value": "America/Tijuana", "name": "(GMT-08:00) Pacific Time -  Tijuana"},
 {"value": "America/Vancouver", "name": "(GMT-08:00) Pacific Time - Vancouver"},
]

I have user timezone detection set up which returns a timezone string as "America/Los_Angeles"

Using Javascript I want to find the where America/Los_Angeles is in the json object so I can use its "name" to prefill a form field.

I am familiar with indexOf() method, but can't work out how to use it in this situation. Is there a simple way to handle this or should I just foreach through the whole list?

Share Improve this question edited Mar 30, 2018 at 13:10 axiac 72.2k11 gold badges116 silver badges145 bronze badges asked Mar 30, 2018 at 13:04 skribeskribe 3,6154 gold badges29 silver badges36 bronze badges 3
  • why foreach, there is filter – Ivan Commented Mar 30, 2018 at 13:05
  • Looks like a job for filter method => developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – Pa Ye Commented Mar 30, 2018 at 13:08
  • @Ivan. Thanks, filter is better. Why foreach? Because I work mostly in the backend and am more familiar with foreach and my javascript skills are a bit underdeveloped. – skribe Commented Mar 30, 2018 at 13:10
Add a comment  | 

4 Answers 4

Reset to default 14

Using Javascript I want to find the where America/Los_Angeles is in the json object so I can use its "name" to prefill a form field.

You can use findIndex

var index = arr.findIndex( s => s.value == "America/Los_Angeles" )

and now use this index to prefill a field.

Or simply use find to return an object

var element = arr.find( s => s.value == "America/Los_Angeles" )

and set the name field in the element itself

element.name = "somevalue";

You can use filter

var arr = [
    {"value": "Pacific/Niue", "name": "(GMT-11:00) Niue"},
    {"value": "Pacific/Pago_Pago", "name": "(GMT-11:00) Pago Pago"},
    {"value": "Pacific/Honolulu", "name": "(GMT-10:00) Hawaii Time"},
    {"value": "Pacific/Rarotonga", "name": "(GMT-10:00) Rarotonga"},
    {"value": "Pacific/Tahiti", "name": "(GMT-10:00) Tahiti"},
    {"value": "Pacific/Marquesas", "name": "(GMT-09:30) Marquesas"},
    {"value": "America/Anchorage", "name": "(GMT-09:00) Alaska Time"},
    {"value": "Pacific/Gambier", "name": "(GMT-09:00) Gambier"},
    {"value": "America/Los_Angeles", "name": "(GMT-08:00) Pacific Time"},
    {"value": "America/Tijuana", "name": "(GMT-08:00) Pacific Time -  Tijuana"},
    {"value": "America/Vancouver", "name": "(GMT-08:00) Pacific Time - Vancouver"},
]

var search = "America/Los_Angeles";
	
var result = arr.filter(o=>o.value === search);
	
console.log( result );

Doc: filter

See the builtin Array.prototype.findIndex(), e.g.:

[{x:1},{x:2},{x:3}].findIndex(o => o.x == 2) // => 1

If targeting an older JavaScript interpreter which does not have that method you could simply iterate over the array and return the index of the first item that matches as demonstrated in this function:

function findIndexByProperty(array, name, value) {
  for (var i = 0; i < array.length; i++) {
    if (array[i][name] === value) { return i; }
  }
  return -1;
}

use JSON.parse to convert it into an array. and then iterate on the array to find the field.

发布评论

评论列表(0)

  1. 暂无评论