Adding key value pairs to all objects [duplicate] - javascript

This question already has answers here:
Add key value pair to all objects in array
(12 answers)
Closed 5 years ago.
Im new to spring, I convert a object as json to use in jsp page. The json object looks like following.
[
{"year":2014,"average":115.5},
{"year":2016,"average":0.0},
{"year":2017,"average":8.28},
{"year":2018,"average":0.0}
]
I need to add another key value pair ( "color" : value ) to each object like given below.
[
{"year":2014,"average":115.5,"color":1},
{"year":2016,"average":0.0,"color":2},
{"year":2017,"average":8.28,"color":3},
{"year":2018,"average":0.0,"color":4}
]
Now I passed to page, how to add this key value pair to each objects in javascript?

Add to your existing object like so:
var yourNewArray = yourOriginalArray.map(function(obj) {
var me = Object.assign({}, obj);
me.color = 'yourNewColorValue';
return me;
});

Related

get a list of values from a specific js object key [duplicate]

This question already has answers here:
Merge arrays to one array after filtering
(5 answers)
Closed 1 year ago.
I have a JavaScript object build as a list of dictionaries.
example of a possible data set:
object =
[
{'name':'abc', 'type':'manual', 'some_id':'d23d', 'assigned_items':['this1']},
{'name':'hef', 'type':'manual', 'some_id':'3hhs', 'assigned_items':['this2, this3']},
{'name':'kuy', 'type':'manual', 'some_id':'k7fd', 'assigned_items':['this4']},
]
I am trying to get a list of all the assigned_items, which will sometimes be more than one.
so the output I would like for this data set will be:
result = ['this1', 'this2', 'this3', 'this4']
I tried the answers explaind here, but it did not work
Javascript equivalent of Python's values() dictionary method
const input = [{'name':'abc', 'type':'manual', 'some_id':'d23d','assigned_items':['this1']},{'name':'hef', 'type':'manual', 'some_id':'3hhs', 'assigned_items':['this2', 'this3']},{'name':'kuy', 'type':'manual', 'some_id':'k7fd','assigned_items':['this4']},]
const res = input.flatMap(e => e.assigned_items)
console.log(res)

how to push object into an nested array within a loop? [duplicate]

This question already has answers here:
Push is overwriting previous data in array
(2 answers)
Closed 2 years ago.
I am using an angularjs foreach and want to find data objects to load into another objects array.
When doing this, it seems like I am adding the variable and all the added objects change to the last variable values.
var p = {};
angular.forEach(d, function(personnel, index){
if(wo.doc.job === personnel.job){
p["EmployeeID"] = personnel.EmployeeID;
p["Number"] = personnel.Number;
wo.doc.personnel.push(p);
console.log(personnel);
}
});
If this finds to 2 employees for a job, they are added and as i watch the wo.doc object after the second object is added the 2 added objects are the same as the last object.
Make a new object in the loop.
angular.forEach(d, function(personnel, index){
if(wo.doc.WorkOrderDetailID === personnel.PrimeWorkOrderNum){
var p = {EmployeeID: personnel.EmployeeID, Number: personnel.Number};
wo.doc.personnel.push(p);
}
});

JSON object to Array Javascript [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 2 years ago.
Im seeing a lot of answers to this in simple arrays but I need the keys and value. I have a json object but need an array. The object looks like this:
{"Food":"Starbucks", "Job":"Electrician"}
I just need a simple array like this:
["Food" => "Starbucks", "Job" => "Electrician"]
Edit: Im not sure how to type it out. Just need to be able to do an array.map and get the keys and values in javascript.
Here is the final code that I am trying:
const details = jsonObject;
{details.map(function(item,idx){
return<DetailCell>
<Label>{idx}</Label>
<Text style={TextStyle}>{item}</Text>
</DetailCell>
})}
Label should be the key and the Text should be the value.
I think you need this :
[{"Food" : "Starbucks"}, {"Job" : "Electrician"}]
To declare a literal array value is something like this in JSON in node.
{JSONVarWithArrayValue : [0,0,0,0]}

How to read Object? [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 5 years ago.
Say i have object like
var a = {"user":
{'average':
{'score':4
}
}
}
How can I read object value using its keys
Say I have user Object with me and have key "average.score" can I get the value directly?
a.user["average.score"];
//Coming as undefined
a.user["average"]["score"]
// Working as expected : 4
I have the key of "average.score" all together with me want to get the value of score how can I do it directly without splitting the key.
Use a.user["average"].score
var a = {"user":
{'average':
{'score':4
}
}
}
console.log(a.user["average"].score);

How to get all values in a specific property within a list of objects [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 7 years ago.
I have an array that looks like this:
var array = [{name:"AName1", value: 1},{name:"AName2", value: 2}, ...];
How do I get all the values from a specific property? Say, I want to get all the names from every object in the array, creating a new array with those names ["AName1, "AName2", ...]
I've tried to use _.pick from underscore.js:
var result = _.map(array, function (current) {
return _.pick(current, 'Name');
});
but it creates another array of objects with only the name property, which is not what i want to do
Any help is appreciated, thanks!
Using map like this:
array.map(function(item){ return item.name; });
The map() method creates a new array with the results of calling a
provided function on every element in this array.

Categories

Resources