What is the process of extracting a value from an array of objects based on another parameter?

Given the array below which contains objects with nested arrays, my goal is to retrieve the value 'AUS Eastern Standard Time' when 'Australia/Melbourne' is passed to the array.

Although I attempted to use the code

var winTimeZone = arr.find(tz => tz.utc && tz.utc.includes('Australia/Melbourne));

The Servicenow platform does not support the find method. I would appreciate any alternative solutions. Thank you

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"
    }]

Answer №1

I am currently experiencing issues with your code, but I find it surprising that the Platform teams are restricting such a basic feature of JavaScript. Perhaps it's time to consider finding a new company that values innovation and development.

Here is a revised version of your code to try:

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"))

Answer №2

This particular piece of code utilizes outdated syntax that is likely to still be compatible:

var locations = [
  {
    "isDaylightSavingTime": false,
    "timeOffset": "09:30:00",
    "locationText": "(UTC+09:30) Darwin",
    "relatedUTC": [
      "Australia/North",
      "Australia/Darwin"
    ],
    "timeZoneValue": "AUS Central Standard Time"
  },
  {
    "isDaylightSavingTime": true,
    "timeOffset": "10:00:00",
    "locationText": "(UTC+10:00) Canberra, Melbourne, Sydney",
    "relatedUTC": [
      "Australia/Sydney",
      "Australia/Melbourne",
      "Australia/Hobart",
      "Australia/Victoria",
      "Australia/ACT",
      "Australia/Canberra",
      "Australia/NSW",
      "Australia/Tasmania",
      "Australia/Currie"
    ],
    "timeZoneValue": "AUS Eastern Standard Time"
  }
];

var winTimeZone;
for (var ind = 0; ind < locations.length; ind++) {
  var tz = locations[ind];
  for (var locInd = 0; locInd < tz.relatedUTC.length; locInd++) {
    if (tz.relatedUTC.indexOf("Australia/Melbourne") > -1) {
      winTimeZone = tz.timeZoneValue;
    }
  }
}
console.log("winTimeZone: " + winTimeZone);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Combining Entities Using Immutable JavaScript

Currently utilizing React, Redux, and Immutable. I have a question about merging a plain JavaScript object into an Immutable Map object. Importing Immutable.js: import { List as iList, Map as iMap } from "immutable"; The content of action.paylo ...

Converting every element in an array into individual hash values?

Query for beginners: I am working with an array called data. It is made up of an array of hashes extracted from a CSV file: [ {:status=>"new", :number=>"215", :subject=>"25", :case=>"First", :attachment=>"alpha, beta"}, {:status=>"old", ...

Transform an array into an object with assigned key names

How can we transform the following array: [2019,2020,2021] into the format: { 0: {year:2019}, 1: {year:2020}, 2: {year:2021} } ...

Set values for scope variables that are created dynamically

I am currently working on a solution to toggle dynamically generated rows of data. I have attempted using ng-init and passing it to a function, but I seem to be making a mistake somewhere and struggling to understand if this is even feasible. The issue see ...

Transferring HTML information to Flask

I'm struggling with passing a value from a text box in a web application to a Flask application. Despite my efforts, the request object in Flask does not seem to contain the data I need. Can anyone provide assistance with this issue? Below are the rel ...

Clicking on the icon reveals the current date

I need some help with the input field that displays the calendar date. Currently, I can only click on the input field to show the calendar date. What I actually want is for users to be able to click on a calendar icon to display the calendar date, which sh ...

Issues with javascript functionality in Internet Explorer version 9 and higher

Currently, there is a flash music player (audioplay) embedded on a website. Despite personal preferences against having music on websites, it was requested by the client. The functionality in question involves using JavaScript to trigger "stop" and "play," ...

Unexpected behavior with VueJS Select2 directive not triggering @change event

Recently, I implemented the Select2 directive for VueJS 1.0.15 by following the example provided on their official page. However, I am facing an issue where I am unable to capture the @change event. Here is the HTML code snippet: <select v-select="ite ...

Unique title: "Implementing Unique Event Handlers with VueJS Components"

My VueJS and Buefy project begins with two distinct click actions: Click on the Cyan area -> redirects to another page (Action 1) Click on the Magenta area -> shows a dropdown menu (Action 2) https://i.stack.imgur.com/AVLOS.png However, when clic ...

Can applications on Windows 8 operate using JavaScript, HTML5, and CSS3 that adhere to industry standards?

As a .NET developer, I tuned in to the keynote for the Build Event in Anaheim, California and had some questions regarding the new support for creating Windows 8 applications using JavaScript, HTML5, and CSS3. They showcased several examples and mentioned ...

Guide to building a nested React component

My custom dropdown component requires 2 props: trigger (to activate the dropdown) list (content to display in the dropdown) Below is the implementation of my component: import { useLayer } from "react-laag"; import { ReactElement, useState } fr ...

When attempting to use JQuery autocomplete, the loading process continues indefinitely without successfully triggering the intended function

Currently, I am utilizing JQuery autocomplete to invoke a PHP function via AJAX. Below is the code snippet I am working with: $("#client").autocomplete("get_course_list.php", { width: 260, matchContains: true, selectFirst: false }); Upon execution, ...

Is it possible for an ajax request to complete even if a page redirection occurs?

In my current project, I am attempting to generate a temporary URL for a local image and submit it to Google for an Image Search. I need this URL to be transient and automatically deleted after the search is complete. Here's the code snippet that I ha ...

Tips on incorporating a new stdClass Object

I am currently facing some confusion with adding new data to an array using the array push method. It seems that it is not working for me at the moment. Can anyone provide assistance? Array ( [0] => stdClass Object ( [name] => Knijn zijn ...

Determine the presence of either one or both values in a JavaScript object

I was looking at an Object like this: {"user": { "name": "Harry Peter", "phoneNumber": "12345", "products": [ { "type": "card", "accountId": "5299367", }, { "type": "Loan", ...

Frames of Animation

Seeking assistance in understanding how to create frame animation using JavaScript or CSS. Attempting to replicate the animation seen on this website. Intrigued by the road animation and unsure if it is achieved using a plugin or just JavaScript and CSS. ...

Is there an effective way to merge two collections?

I came across an issue where I am attempting to merge two arrays that resemble the ones listed below: var participants = [ {id: 1, name: "abe"}, {id:2, name:"joe"} ]; var results = [ ...

Success/Fail Page for Form Redirect

I've been struggling to figure out how to redirect my form to a success or fail page. I've scoured the internet for solutions, looking into traditional form redirects and even JavaScript onClick redirects. Can someone guide me on adding a redirec ...

Preventing Canvas Image Disappearance with JavaScript's onload Event

My current issue involves adding an Image to my webpage. However, every time I hit the refresh button, the image disappears. After researching on Stack Overflow, it was suggested to include window.onload=yourDrawFunction() in the code. Despite following th ...

Is there a way to prevent the "undefined" message from displaying in the console when this code is run?

Help needed! Can someone assist me in resolving the issue where the word 'undefined' is being displayed in the console? I'm a beginner in programming and struggling with this. Here's what I'm currently seeing: You are not getti ...