This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 8 years ago.
I am trying to figure out a way to add field names and values to an object.
for example I hav ethe following...
$scope.product = {
"Requirements": [
{
"OriginPostcode": '',
"BearerSize": 100,
"BandwidthRequired": 10
}
]
}
And I want to add two more names and values...
"Term": 36,
"Quantity": 1
I know the push() function is for arrays. What do you uses for objects?
Many thanks
You can use baces passing index like an array:
$scope.product["Requirements"][0]["Term"] = 36
$scope.product["Requirements"][0]["Quantity"] = 1
you can also do in this way
$scope.product["Requirements"][0].Term = 36
$scope.product["Requirements"][0].Quantity = 1
and also...
$scope.product.Requirements[0].Term = 36
$scope.product.Requirements[0].Quantity = 1
What do you need to understand is how to go through a javascript object using braces [] or point . Take a look at this link
Related
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 1 year ago.
I have an object of objects in which I want to print all the name values, how can I do this easily without having to hardcode each one?
const myObj = {
results: {
one: {
name: "o",
value: 1
}
two: {
name: "t",
value: 2
}
}
ignore: "this"
}
I can make it work with myObj.results.one.name, but I'd rather be able to do something like a loop where I can swap out the middle value like results.[1].name preventing the hard coding of 20 values.
Is there a simple way to do this without hardcoding each one. (The end use is to add them all to an array and use that to populate a table to show name and values)
This linked answer doesn't answer my question as this is an array of objects so solutions fail, and the second isn't nested
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)
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
let arr = [{
question:{
q1 : 123,
q2 : 44
}
}];
I've tried using forEach loop to do it, but it returns nothing.
If it's an array then you can do either map or forEach.
arr.forEach()
However you have to pay attention to the internal structure of the item as well, ex. in your case
arr.forEach(item => {
console.log(item.question.q1)
})
And if you want the transformation, then
const transformedArr = arr.map(item => item.question.q1)
This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 5 years ago.
let's suppose I have the following object:
k = {
name : 'Sam',
age : 20,
interests : " ... "
friends: " ... "
};
How to get all the objects properties without using the Object.keys() function in the ouptup format:
['name', 'age', 'interests' ...]
Note: don't mark my question as duplicate and provide a link to an answer with the Object.keys() function
var keys = [];
for (var key in k){
keys.push(key);
}
For more complicated objects than your example, you may want to check k.hasOwnProperty(key) in that loop before adding it.
This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 8 years ago.
I need to print all datas in an object in a for loop but I don't know how to access to their index.
For example I have an object like this (this is an example ,but the real one is very big)
a = { name : richard ,last_name : stallman };
I can do this :
cnosole.log(a.name);
cnosole.log(a.last_name);
But the object is too big.How can I do this?
Our you could do it like this:
a = { name : 'richard' ,last_name : 'stallman' };
for( key in a){
console.log(a[key]);
}
You can convert an object to a string with JSON.stringify.
console.log(JSON.stringify(a));
should print something like:
{"name": "richard", "last_name": "stallman"}