如何根据其他参数从对象数组中获取值?



考虑下面的数组具有对象,并且该对象也具有数组。我要找的是,如果我传递'澳大利亚/墨尔本'到下面的数组输出应该是'澳大利亚东部标准时间'即第二个对象的值元素(作为第二个对象包含值'澳大利亚东部标准时间'在'utc'数组)

注意我试过var winTimeZone = arr.find(tz => tz.utc && tz.utc.includes('Australia/Melbourne));

Servicenow平台不支持find方法。请分享其他解决方案。由于

var arr =[
{
"isdst": false,
"offset": "09:30:00",
"text": "(UTC+09:30) Darwin",
"utc": [
"Australia/North",
"Australia/Darwin"
],
"value": "AUS Central Standard Time"
},
{
"isdst": true,
"offset": "10:00:00",
"text": "(UTC+10:00) Canberra, Melbourne, Sydney",
"utc": [
"Australia/Sydney",
"Australia/Melbourne",
"Australia/Hobart",
"Australia/Victoria",
"Australia/ACT",
"Australia/Canberra",
"Australia/NSW",
"Australia/Tasmania",
"Australia/Currie"
],
"value": "AUS Eastern Standard Time"
}]

我认为你的代码不工作,无论如何。这是我第一次听说平台团队不允许javascript的这样一个基本特性。我最好的建议是换公司。

试试这个:

var arr =[
{
"isdst": false,
"offset": "09:30:00",
"text": "(UTC+09:30) Darwin",
"utc": [
"Australia/North",
"Australia/Darwin"
],
"value": "AUS Central Standard Time"
},
{
"isdst": true,
"offset": "10:00:00",
"text": "(UTC+10:00) Canberra, Melbourne, Sydney",
"utc": [
"Australia/Sydney",
"Australia/Melbourne",
"Australia/Hobart",
"Australia/Victoria",
"Australia/ACT",
"Australia/Canberra",
"Australia/NSW",
"Australia/Tasmania",
"Australia/Currie"
],
"value": "AUS Eastern Standard Time"
}]

const utcValueForCity = (utcCity) => 
arr.reduce((acum, current)=>
acum || (current.utc.includes(utcCity)?current.value:undefined)
, undefined)

console.log(utcValueForCity("Australia/Melbourne"))

这段代码使用了几乎肯定会被支持的旧语法:

var arr = [
{
"isdst": false,
"offset": "09:30:00",
"text": "(UTC+09:30) Darwin",
"utc": [
"Australia/North",
"Australia/Darwin"
],
"value": "AUS Central Standard Time"
},
{
"isdst": true,
"offset": "10:00:00",
"text": "(UTC+10:00) Canberra, Melbourne, Sydney",
"utc": [
"Australia/Sydney",
"Australia/Melbourne",
"Australia/Hobart",
"Australia/Victoria",
"Australia/ACT",
"Australia/Canberra",
"Australia/NSW",
"Australia/Tasmania",
"Australia/Currie"
],
"value": "AUS Eastern Standard Time"
}
];
var winTimeZone;
for (var dataInd = 0; dataInd < arr.length; dataInd++) {
var tz = arr[dataInd];
for (var locationsInd = 0; locationsInd < tz.utc.length; locationsInd++) {
if (tz.utc.indexOf("Australia/Melbourne") > -1) {
winTimeZone = tz.value;
}
}
}
console.log("winTimeZone: " + winTimeZone);

相关内容

  • 没有找到相关文章

最新更新