Extract particular elements from an array and transform them into a new array

Presented below is an array:

[{
"title": "Apple iPhone 7 Plus 32 GB",
"category": "phone",
"brand": "apple",
"condition": "Used",
"price": 800,
"id": 0,
"description": "Apple"
}, {
    "title": "Apple Ipad Air 32 GB",
    "category": "tablet",

    "brand": "apple",
    "condition": "new",
    "price": 1000,
    "id": 1,
    "description": "Apple"
},{
    "title": "Supreme Box Logo Hooded Sweatshirt Navy",
    "category": "clothes",
    "brand": "Supreme",
    "condition": "new",
    "price": 1000,
    "id": 2,
    "description": "Supreme Size M"
},]

Is there a method to extract only the values of category from the aforementioned array and transform them into an array like this?

[{"phone","clothes","tablet"}]

Answer №1

Iterate over your array and extract the category of each item.

In addition, [{"phone","clothes","tablet"}] is not valid; an object consists of key-value pairs, so I assume you intended to write ["phone","clothes","tablet"]

const items = [{
"title": "Apple iPhone 7 Plus 32 GB",
"category": "phone",
"brand": "apple",
"condition": "Used",
"price": 800,
"id": 0,
"description": "Apple"
}, {
    "title": "Apple Ipad Air 32 GB",
    "category": "tablet",

    "brand": "apple",
    "condition": "new",
    "price": 1000,
    "id": 1,
    "description": "Apple"
},{
    "title": "Supreme Box Logo Hooded Sweatshirt Navy",
    "category": "clothes",
    "brand": "Supreme",
    "condition": "new",
    "price": 1000,
    "id": 2,
    "description": "Supreme Size M"
},];

const newItems = items.map(item => item.category);
console.log(newItems)

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

Extract data from JSON in Google Sheets

UPDATE: entry.content.$t is actually not the correct field to access individual cells. The proper method is using entry.gsx$[cell column header]. Thank you for pointing out this mistake and assisting in finding a solution. Initial inquiry: I am currently ...

Upon opening index.html in the browser, the jQuery code fails to execute, as no errors are displayed in the console

I am struggling to make a simple toggleClass() function work in jQuery and I can't seem to figure out why. This is just a beginner exercise for me with jQuery. The code works perfectly when I test it as a snippet or manually in the console, but when I ...

Exploring nested tag item retrieval in Python

Currently developing an automation script using selenium to gather job listings from indeed. Here is the link to the worldwide site and a brief description of what is needed. Visit Indeed worldwide here The task involves extracting all location names a ...

The Next.js page suddenly disappears after a moment

Out of the blue, my next.js page suddenly went blank for no apparent reason. I didn't make any changes, it just happened. I attempted to restart my dev server and even deleted the '.next' folder, but nothing seemed to fix the issue. Despite ...

Tips for preventing repetition of code in multiple entry points in Rollup

My goal is to use rollup to process a group of input files and generate multiple output files in the dist directory that all have some common code shared between them. Below is my current rollup configuration: import path from 'path'; import pat ...

Slide in parts gradually by scrolling up and down, avoiding sudden appearance all at once

I have implemented a slider on my website using jQuery functions. For scrolling down, the following code snippet is used: jQuery("#downClick").click(function() { jQuery("html, body").animate({ scrollTop: jQuery(document).height() }, "slow"); ...

What significance does it hold for Mocha's `before()` if the function passed requires parameters or not?

In one part of my code, I have a describe block with before(a) inside. The function a originally looks like this: function a() { return chai.request(app) ... .then(res => { res.blah.should.blah; return Promise.resolve(); }); ...

Foreign keys in a one-to-many relationship with Sequelize.js

I am in the process of developing a survey application using Node.js/Express and MySQL incorporating Sequelize.js ORM. However, I am encountering difficulties while establishing the relationship between the two models. My goal is to have the Questions&apo ...

Create a substitute for Object.seal, Object.freeze, and Object.preventExtensions applications

Is there a way to freeze an object's existing properties while still allowing new ones to be added? I can't seem to find any built-in functionality for Object.freezeExisting(), so maybe it's worth considering implementing it, possibly even w ...

How can JavaScript be utilized to disable the use of the spacebar?

I have implemented a live search feature on the website I'm working on. Currently, the search function queries the MySql database as soon as the first character is entered and updates the results with each subsequent character input. However, I'v ...

What is the process for incorporating an additional input in HTML as you write?

I am looking to create a form with 4 input boxes similar to the layout below: <input type="text" name="txtName" value="Text 1" id="txt" /> <input type="text" name="txtName2" value="Text 2" id="txt" /> <input type="text" name="txtName3" valu ...

Issue when sending PHP Papa Parse object through AJAX

I am currently working on extracting a CSV local file in PHP using AJAX and have found success with Papa Parse. However, I am facing difficulties passing the results with AJAX. Despite trying various methods such as JSON.stringify, passing only the first f ...

What is the best way to limit a plugin to only one version of jQuery when there are 2 versions included on a page?

As I develop a piece of Javascript code to embed into my client's websites, I have concerns about potential conflicts with other versions of jQuery that they may be using. While I have found some guidance in the jQuery documentation, implementing it l ...

Revise Script to Duplicate Alt Attribute onto Miniatures

I'm customizing a gallery plugin for a website that I am currently developing, aiming to add support for titles. The script I am using can be found here: DEMO: http://jquery.malsup.com/cycle/pager2.html CODE: All the functionalities are in place e ...

Ways to convert a JSON file into a ListView using Volley

I'm currently developing an app that requires fetching data from the internet, such as names and email addresses. I followed tutorials on using Volley to parse a JSON file and successfully retrieved the data. However, I encountered an issue when tryin ...

Exploring Unicode Symbols for Icon Selection

I'm currently working on integrating an icon picker into my application that will enable the user to select a mathematical operator for data comparison. While I have successfully implemented the fontawesome-iconpicker on the page, I am encountering d ...

Encasing the app component with a context and encountering the issue: TypeError - (destructured parameter) does not have a defined value

My goal is to wrap all components under the app in a context to provide specific functionalities (as evidenced by my UserContext component). import React, { useState, createContext, useContext } from 'react' const Context = createContext(); exp ...

convert webpages and images to PDF with a custom watermark

Looking to convert images fetched from the server into PDF format with a watermark by clicking a button. It would be ideal if there is a plugin that allows for PDF preview with disabled user selection and copy options. The website's platform is Sales ...

The value of the jQuery data on click event handler becomes 'undefined' when used within an AJAX handler

When I click on the Positive or Negative buttons, a jQuery event handler function is triggered. Each button passes different data objects to the handler. However, within the AJAX POST handler, I am unable to access the data from the click event. $('# ...

How does Vue handle the situation when the value of an input field does not match the bound "data"?

Before diving into the intricacies of v-model, I want to take a closer look at how v-bind behaves. Let's analyze the example below: <div id="app"> <input type="text" :value="inputtedValue" @input ...