Javascript proper way to .push array [duplicate] - javascript

This question already has an answer here:
javascript push returning number instead of object [duplicate]
(1 answer)
Closed 3 months ago.
How to use .push array in proper way using javascript?
my code
let persons = [{
"person1": "person1"
},
{
"person1": "person1"
}
]
let addPerson = []
addPersn.push({
"person1": "person2"
})
let allPerson = persons.push(addPerson)
console.log(allPerson)
expected behavior
{
"person1": "person1"
},
{
"person1": "person1"
},
{
"person1": "person1"
}
current result
3
How to use .push array in proper way using javascript?How to use .push array in proper way using javascript?

Try this
let persons = [
{
person1: "person1",
},
{
person2: "person2",
},
];
const newPerson = {
person3: "person3",
}
persons.push(newPerson);
console.log(persons);

Related

Getting the object from an array on the basis of id [duplicate]

This question already has answers here:
How to find an appropriate object in array by one of its properties
(3 answers)
Closed 29 days ago.
I have a array as follows :
data = [
{
"data": {
"id":1,
"vol":"0.0"
"requiredId":100
"details":{
"ABC":"8.30",
"OFG":"13.85",
"SPG":"70.80"
}
}
},
{
"data": {
"id":2,
"vol":"1.0",
"requiredId":2
"details":{
"ABC":"3.30",
"OFG":"15.85",
"SPG":"70.80"
}
}
}
]
I just want to get the object from this array where id is equal to requiredId. How can I do that?
data.find(item => item.data.id === item.data.requiredId)

how to combine multiple arrays in react and how can we show the map in ascending order [duplicate]

This question already has answers here:
How to merge two array of objects with reactjs?
(4 answers)
Closed 11 months ago.
how to merge array of this in react or javascript..can anyone help me with this i want the output something like..all the test array should be in one array...coz i need to find the minimum price and plus i need to show the mapping in ascending order
array :[
{
id:0,
name:'something',
test: [
{
id: 0,
test_name:'blood',
price:'200',
},
{
id:1,
test_name:'kidney',
price:'300',
}
]
},
{
id:1,
name:'something1',
test: [
{
id: 0,
test_name:'blood2',
price:'100',
},
{
id:1,
test_name:'kidney2',
price:'100',
}
]
}
]
You can use spread operator to merge two array.
var a = [{fname : 'foo'}]
var b = [{lname : 'bar'}]
var c = [...a, ...b] // output is [{fname : 'foo'},{lname : 'bar'}]

Move keys of object to individual properties of child objects, creating an array [duplicate]

This question already has answers here:
How to turn this ID-based objects into an array of objects?
(2 answers)
Closed last year.
The API data looks like this:
{
"00041335": {
"productId": "8e786e6b",
"variantId": "0b43df16"
},
"00032183": {
"productId": "9e780e6d",
"variantId": "0b48df17"
}
}
I want to make it like this:
[
{
"skuId": "00041335",
"productId": "8e786e6b",
"variantId": "0b43df16"
},
{
"skuId": "00032183",
"productId": "9e780e6d",
"variantId": "0b48df17"
}
]
How to restructure the object? Is reduce appropriate or is there a better way?
There might be a better way using more efficient destructuring but this did the trick for me:
taking the first object's key value and inserting it into a new object with property id1. then take the object nested in object1 and spread it over the rest of the object.
let obj1 ={ "00041335332183": { "Id2": "8e786e6b", "Id3": "0b43df16" } }
let keyValue = Object.keys(obj1);
let newObj = {
id1: keyValue[0],
...obj1[keyValue[0]]
}
Easiest way would to use Object.keys()and then you can merge the two objects with the spread operator (...).
const o ={ "00041335332183": { "Id2": "8e786e6b", "Id3": "0b43df16" } }
console.log({ Id1: Object.keys(o)[0], ...Object.values(o)[0] })
First of all, you need to find the first key of your object, in order to do that you can use Objects
Object.keys(yourobject) provides your keys in the array format, out of these keys the first index is your ID.
Now you need to create a new object and merge the rest of the data by using the spread operator (...) Learn from here
let data = { "00041335332183": { "Id2": "8e786e6b", "Id3": "0b43df16" } }
let parsedObject = {id: Object.keys(data)[0], ...data[Object.keys(data)[0]]}
console.log (parsedObject)

JavaScript Build Arrays from Array [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 1 year ago.
I currently have an array that has 2 levels. I am attempting to build two new arrays from the initial array but seem to be stuck with the method to use. I have attempted a forEach(), while loop, as well as push to no avail.
My current array structure:
[
{
"attributes": {
"Summary": 10,
"class": "Blue"
}
},
{
"attributes": {
"Summary": 63,
"class":"Red"
}
}
]
I am looking to build two arrays, namely one for summary values, and one for class values.
Are my approaches of a forEach or while loop on the correct path?
If you have an array and want to transform that into another array, the simplest way is to use map (which basically creates a new array containing the results of running each element through a function):
const arr = [
{
"attributes": {
"Summary": 10,
"class": "Blue"
}
},
{
"attributes": {
"Summary": 63,
"class":"Red"
}
}
];
const summaries = arr.map(e => e.attributes.Summary);
const classes = arr.map(e => e.attributes.class);
If you want to accomplish this using forEach, here's one way to do it:
const aryInitial = [
{
"attributes": {
"Summary": 10,
"class": "Blue"
}
},
{
"attributes": {
"Summary": 63,
"class":"Red"
}
}
];
let arySummary = [];
let aryClass = [];
aryInitial.forEach((obj)=>
{
arySummary.push(obj.attributes.Summary);
aryClass.push(obj.attributes.class);
});
console.log("Summary Array:",arySummary);
console.log("Class Array:",aryClass);

Javascript Object traversal with String [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I need to traverse a JavaScript object with a stored string.
Sample string
var x = "Desserts"
Sample Object
{
"dataset":
{
"Categories" :
[
{
"Desserts" :
[
"Sweets","Ice Creams","Pastry"
]
} ,
{
"Juices and Beverages" :
[
"Cold","Hot","Fresh","Sodas"
]
}
}
}
If I traverse the object as dataset.Categories.x, it doesn't work[returns undefined] . How can I do this?
You should use dataset.Categories[0][x] instead of dataset.Categories[0].x
Take a look at : dot notation or the bracket notation
var x = "Desserts",
data = {
"dataset": {
"Categories": [{
"Desserts": ["Sweets", "Ice Creams", "Pastry"]
}, {
"Juices and Beverages": ["Cold", "Hot", "Fresh", "Sodas"]
}]
}
}
alert(data.dataset.Categories[0][x]);
var obj = {
"dataset": {
"Categories": [{
"Desserts": ["Sweets", "Ice Creams", "Pastry"]
}, {
"Juices and Beverages": ["Cold", "Hot", "Fresh", "Sodas"]
}]
}
}
var x = 'Desserts';
var val = obj.dataset.Categories[0][x];
console.log(val);
JSFIDDLE.

Categories