What is the best way to save the properties of elements in an array of objects within another array?

I have obtained attributes from objects within an array that I need to store in another array. Here is the data I am working with:

https://i.sstatic.net/b0JtY.jpg

My goal is to extract the `displays` name attribute and save it in the `opt[]` array, which would result in something like this:

opt = ['info1', 'info2', 'info3', ... ]

getEditData (id) {

            axios.get('/api/campaign/getEdit/' + id)
                .then(response =>{
                    this.campaign = response.data.campaign;
                })
                .catch(e=>{
                    console.log(e.data);
                    this.error = e.data
                })
        }

The snippet above shows how the campaign object is being sourced.

Answer №1

Here is one way you can extract names from the campaigns:

campaigns.displays.map(({name}) => name );

const campaigns = { displays: [{ name: 'example1'}, { name: 'example2'}] };

const results = campaigns.displays.map(({name}) => name );

console.log(results);

Answer №2

In the following code snippet, an array is displayed that contains the property names of each object in the displays array

    var info = {
      displays: [
        {
          capacity: 9000,
          id: 1,
          imei: 44596
        }
      ]
    };
    info.displays.forEach(function(object, index) {
      console.log(Object.keys(object));
    });

Object.keys() is a helpful method to achieve this functionality

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 issue with file upload feature in Angular for Internet Explorer 9

I have implemented a file upload method using the following code: <input type="file" name="upload-file" ng-model= "excelFile" accept=".xlsx" onchange="angular.element(this).scope().fileChanged(this);" ...

Are third-party scripts and HTML widgets able to replicate your website's data, including cookies, HTML, and other elements?

Currently, I am in the process of constructing a website that incorporates a third-party weather HTML widget. The widget is sourced from a trusted and reliable source on the web. It consists of a link and small JavaScript tags that are executed once loaded ...

Regular expressions tailored for a precise format in JavaScript

Is it possible to create a regex that can validate a specific format? For example, if I have version numbers like v1.0 or v2.0 v1.0 or v2.0 My current regex expression only validates the existence of v, a number, or a .. How can I implement validation ...

The python-pyinstrument is in need of a javascript dependency that seems to

As I attempt to profile my Python program using pyinstrument, I encounter an error when trying to view the profile in HTML format. Traceback (most recent call last): File "/home/ananda/projects/product_pred/025200812_cpall_ai_ordering_model_v2/.venv ...

Is it viable to execute a reload on the same location and subsequently activate a click function?

Is it possible to combine a click function with a location reload and then trigger another click function? Please see the code example below: $('.ui-button').click(function () { location.reload(); $('#Sites').t ...

Enter key triggering input change event is unresponsive in Internet Explorer

$("#inputBoxWidth").change(function() { var curValue = $("#inputBoxWidth").val(); alert(curValue); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input type="text" id="inputBo ...

best way to sort through an array using jquery in javascript

Is there a way to filter a jQuery array like in SQL server with "Like % %"? var array=[ {"job_category":"hello sir","job_location":"hello dear"}, {"job_category":"dear kumar ","job_location":"sir"}, {"job_category":"testts ssss ss","job_location":"hello t ...

Numpy: Executing in-place operations on a dynamic axis

After much consideration, I have tried my best to outline the issue in the title. The problem at hand is the variability of a numpy array's shape or dimension (which can range from 1 to 3). For instance, in the scenario where the array is of shape [1 ...

Error: Unable to access attributes of an unknown variable (retrieving 'use')

I encountered an issue (TypeError: Cannot read properties of undefined (reading 'use')) while trying to execute the 'node server.js' command in the Terminal. The error points to my auth.routes.js file. https://i.sstatic.net/NQ5XL.png ...

How to disable the ripple effect of a parent button in Material UI when clicking on a nested child button?

Attempting to nest one button within another (IconButton inside ListItem with the button prop) is proving challenging. The issue lies in the fact that the ripple animation of the ListItem is triggered even when clicking on the IconButton. Ideally, I would ...

Issue with AngularJS factory $http.get request receiving HTML files as response

Could someone please explain why I keep receiving an HTML file as a return from my Angular factory? This is the route on my backend: function ensureAuthenticated(req, res, next) { if (!req.headers.authorization) { return res.status(401).send({ mess ...

Exploring Nested Views in a MEAN Application using UI Router

I am currently developing a MEAN application and struggling to get my ui-router functioning correctly. Within my index.html template, I have loaded all the necessary javascript and css for my application such as angular, jquery, angular-ui-x, bootstrap. I ...

How to use JQuery to parse an external JSON file with array elements in Javascript

My goal is to extract information from an external JSON file using JavaScript, specifically an array and other elements. The JSON file I am working with is called 'TotalUsers.json' {"@version":"1.0", "@generatedDate":"12/20/10 5:24 PM", "day":[{ ...

JavaScript function unable to execute form action properly

I have a link RESET YEAR which triggers a servlet to check if the current year is equal to the present year. If they are not equal, then the function resetyear() is supposed to be called. The issue I am facing is that the function is not working as expecte ...

Animate an element when switching routes

Is there a way to smoothly transition an SVG element across a page when the route changes in Vue.js? I've attempted to set up a watcher that triggers an animation based on the route path conditions. Although the transitionName updates correctly, the ...

Tips for utilizing the value of object1.property as a property for object2

Within the template of my angular component, I am attempting to accomplish the following: <div> {{object1.some_property.(get value from object2.property and use it here, as it is a property of object1)}} </div> Is there a way to achieve this ...

I found myself pondering the significance of the {blogs: blogs} in my code

app.get("/articles", function(req, res){ Article.find({}, function(err, articles){ if(err){ console.log("an error occurred!!!"); }else{ res.render("homepage", `{articles: articles}`); } }); I created this c ...

The rotation function of a THREE.js object seems to be malfunctioning

Currently, I am facing an issue with a Blender object that I have successfully displayed on my web page using THREE.js. However, for some reason the object is not rotating when my loop function is called. In my approach to working with JavaScript, I am tr ...

Enhance your figures with a unique Javascript magnifying tool that works seamlessly across all browsers

After searching the web for magnifying glasses, I found that most only work for one picture. So, I took matters into my own hands and created a magnifying glass that can magnify all pictures within a specific div. It functions perfectly on Chrome browser b ...

Implementing automatic pagination based on container height using jQuery

Looking to convert an AngularJS fiddle into jQuery. The goal is to create pagination using several p tags only with jQuery. Current code: <div class="parent"> <p>text1</p> <p>text2</p> <p>text3</p> ...