How to transform a JSON object into an array using JavaScript

Is there a way to convert a JSON key value object into an Array that is easier to iterate through? The JSON I have looks something like this:

{
  "01": "yes",
  "02": "yes",
  "03": "no"
}

However, I need the Array format below in order to easily iterate through it:

["01:yes","02:yes","03:no"]

Alternatively, is there a way to iterate through this JSON object while being able to access the keys and values without any complications?

Answer №1

Utilize the power of Array#reduce

Object.keys() generates an array containing the object's own enumerable properties

var obj = {
  "01": "yes",
  "02": "yes",
  "03": "no"
};
var op = Object.keys(obj).reduce(function(a, b) {
  return a.concat(b + ':' + obj[b]);
}, []);
console.log(op);

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

Troubleshooting Azure Routing Issues after Switching to .ejs Files

I am facing an issue where my application breaks on Azure after changing the file extensions and routes to point to the renamed .ejs files, but it works perfectly fine locally. angularApp.js var app = angular.module('theApp', ['ui.router&a ...

Angular ng-show does not seem to evaluate properly

After receiving the JSON array object below: "Tools": [ { "name": "Submit a Claim", "position": 1, "isOn": true, "alert": null }, { "name": "My Recurring Claims", "position": 2, "isOn": true, "alert": null }, { "name": "Online Enrollment ...

Template for Gatsby's alternative layout design

I have recently developed a template for pages that showcase a single product (blog-post.js). However, I now require a different type of page with its own template (category-post.js) to display all products within a specific category. Since the number of c ...

Sending a parameter to a confidential npm module

I've developed a custom npm module that can be used in my applications by including this code snippet in the HTML: <app-header-name></app-header-name> Here is the TypeScript code inside the npm package: import { Component, OnInit } from & ...

Using JavaScript requests to save XML data and PHP to read and process the XML data stored in MySQL

I have encountered an issue while trying to save XML content, received as plain text, into my site's database. I came across advice suggesting not to store XML in a text field but instead use a blob. So, I proceeded to save it as a blob using CORS and ...

Displaying numbers as characters in AngularJS: Use $index variable

Within my ng-repeat, I have two <li> tags repeating a specific number of times. The $index is being used to determine the position of each <li>. You can view a sample on this fiddle: http://jsfiddle.net/sh0ber/cHQLH However, I would like one ...

JQuery Function for Repeatedly Looping through Div Elements

I've been experimenting with creating a loop that alternates the fade-in and fade-out effects for different elements. This is what I have so far: setInterval(function() { jQuery(".loop").each(function(index, k) { jQuery(this).delay(1200 ...

Updating device information in real-time using React Native

Currently, I am utilizing react-native-device-info to access the DeviceLocale or DeviceCountry. However, I am wondering if there is a method to update Device-info without requiring a complete restart of the app. For instance, when my device language is se ...

Accessing data from nested arrays in PHPHere are ways to

Currently, I am making API calls. The response I receive is in the form of an associative array, allowing me to access values like this: $field = $response['nameOfKey']; However, some keys have values that are arrays themselves. Take a look at ...

Button is nested within a closing div causing it to trigger independently without affecting the parent

I have implemented a slideup and slidedown system with some fading effects, but I am encountering an issue with the slide up function. It doesn't seem to reset the display property back to none. For reference, here is a Fiddle link: Slide Up/Down Fid ...

What is the method for configuring Express to serve static files from a parent directory?

If I have a nodejs express application with the following folder structure: -app - frontend - assets - images - scripts - styles - ... - pages - backend - server.js How can I serve the static files in the assets fold ...

The Interactive Menu Toggler: A jQuery Solution

$(window).on('resize', function() { if ( $( window ).width() > 768 ) { $('#menu-main-navigation').show(); } }); $('#nav-toggle').on('click', function() { // start of the nav toggle $('#m ...

I am looking for a way to display or conceal text depending on the presence of an image

Could you please help me with this? I am looking for a solution where text is displayed only when there is content in the src attribute of an img tag. Essentially, I need JavaScript code that can identify if an attribute has content and then make a paragra ...

Swipe to eliminate an element in Ruby on Rails

I am looking to implement a drag-and-drop delete feature on my website, similar to the recycle bin/trash function on Windows or OSX. Within my database, I have multiple objects represented by div elements using Ruby. While I know how to add drag functiona ...

Using jQuery to gently fade out text at the top and bottom of the page as you scroll

I am seeking a way to fade out the content of my page with an opacity/rgba color effect as it approaches a specific distance from both the top and bottom of the viewport. This is the desired outcome I want: In the example above, there is a gradient posit ...

Dependencies in Angular 2 are essential for efficient functioning

Let's examine the dependencies listed in package.json: "dependencies": { "@angular/common": "2.2.1", "@angular/compiler": "2.2.1", "@angular/core": "2.2.1", "@angular/forms": "2. ...

Building pagination functionality in AngularJS by sending parameters to an AJAX service: A step-by-step guide

I'm looking to learn how to implement pagination on a page with 10 records per page. Despite my efforts in searching online, I haven't been able to find any helpful resources explaining this concept. I would greatly appreciate a simple example or ...

Delete a nested JSON object that contains a specific key and value using Jackson library

I am looking to eliminate blob columns from JSON objects by checking for the presence of "@type": "blob". If any object contains a blob column, it should be removed. For example, consider a record with columns 'experience', 'hitpoints', ...

Combining React with a jQuery plugin

Utilizing the jQuery nestable plugin in my React App has been a lifesaver for meeting my business needs. Despite being aware of the potential complications that arise from mixing jQuery with React, I couldn't find the exact functionality I required in ...

"Enhance your web development with Vue.js and Vue-chart.js for beautiful linear

I'm currently struggling to implement a linear gradient background on my Vue-chart.js line chart. Despite searching high and low, the documentation and examples available are not proving to be helpful. After importing the Line component from vue-char ...