Take a subset from a complex object in JS [duplicate] - javascript

This question already has answers here:
How to map more than one property from an array of objects [duplicate]
(7 answers)
Closed 1 year ago.
How can I take (in JS) all the values inside this nested object except for the last two elements (scenario and total). here an image of the object I have:
I need an array equal to this one, but without "scenario" and "total" in the 3 nested objects inside.

Use map function
array.map(item => {
delete item.scenario;
delete item.total;
return item;
});

Related

how to create an array of values from an existing array in Javascript [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 5 months ago.
I have an array which has many objects, I want to make an array of tag values so I can display the non repeated tags in the frontend.
This is the array, and from which I want to create the new array which will include all the tag values.
Can you please share with me a solution or maybe a way to solve this ? Thanks
You can use Array#map to get all the tag property values, and then create a Set from that to get the unique ones.
const tags = [...new Set(yourObj.nodes.map(x => x.tag))];

"indefined selection console.log(data[])" [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
I can recover my api but I cannot recover firstname in my object people, do you have a solution?
here my code for select l'object people :
console.log(data['people']);
and i want select firstname in my object people
Your people object is an array so, to get the firstname value you need to either loop in this array (with foreach or map) and then use .firstname, or you can just specify an entry
so you can do :
data['people'][0].firstname
or
data['people'].forEach(element => {
element.firstname
});
can you loop the array by using map.
data['people'].map((item, index) => {
console.log(item.firstname)
})

How to merge two arrays at run time [duplicate]

This question already has answers here:
Why Doesn't Array Concatenation Work in Javascript? [closed]
(3 answers)
Closed 2 years ago.
I am getting below like values at runtime. Below is for only example. I will have same array structure at runtime. I want to merge them.
let finalSearchResult =[];
data [{"a":1000,"a":1000001,"a":10000002,"D":5000000}]
data [{"P":1000,"Q":1000001,"R":10000002,"S":5000000}]
finalSearchResult.concat(finalSearchResult,data);
but its not working. When I am printing finalSearchResult its coming as null.
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
please refer to:
Array.prototype.concat documentation
Therefore:
finalSearchResult = finalSearchResult.concat(data);
Will be the correct way to concat the arrays.

Get all elements from all subarrays present in an array without removing element from the principal array? [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Transform Javascript subArrays into one Array [duplicate]
(4 answers)
Closed 3 years ago.
I work on a project with a lot of arrays and in some case I have this:
let main_array = [a,b,c,d,[e,f,g],h,i,j,[k,l,m,o],[p],q,[]]
In my main array I have a lot of subarrays, and I want to get all element from them without removing the other elements from the main array. So I want a function which can do this:
f(main_array) --> [a,b,c,d,e,f,g,h,i,j,k,l,m,o,p,q]
The particularity with the main array is about the number of arrays inside it, in fact, it could have 0 to n subarray(s) with 0 to n element(s).
Is there an existing built-in function or just a simple function with filter for example that can solve this problem in a few line of code (just 1 could be great !) ?
PS: I use node.js

Javascript object traversal in wrong way [duplicate]

This question already has answers here:
How to keep an Javascript object/array ordered while also maintaining key lookups?
(2 answers)
Closed 7 years ago.
I have a json object in occupancy_list as follows :
I am traversing the object as follows :
for(var prop in occupancy_list)
{
console.log(prop);
}
I am getting values in reverse order. Like at first Room 2 then Room 1. How can I fix it ?
Object properties are unsorted and could always be random. If you need order - get the keys, and iterate them that way:
Object.keys(occupancy_list).sort(function(a, b) {
//sort logic
}).forEach(key) {
//logic on each key
});

Categories