This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 3 years ago.
I want to loop through a javascript object passed in like this:
{familyid:434832,groupid:5332,programtypecode:'daycare'}
But I don't know the parameter names. There could be any number of parameters with various different names. I want to get the names of the parameters passed in, and of course also their values. How do I get that?
Use Object.keys()
const o = {
familyid: 434832,
groupid: 5332,
programtypecode: 'daycare'
}
keys = Object.keys(o)
// Do something with your keys, like
for (key of keys) {
console.log(`${key} => ${o[key]}`)
}
If you want the result in form of array of array of arrays then use Object.entries
let obj = {familyid:434832,groupid:5332,programtypecode:'daycare'}
console.log(Object.entries(obj))
If you want to directly loop then use for..in
let obj = {familyid:434832,groupid:5332,programtypecode:'daycare'}
for(let k in obj){
console.log(`${k}:${obj[k]}`)
}
Related
This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 1 year ago.
Suppose I have an object from that has a key and value pair and I want to reformat it into a simple javascript array:
var obj = {STRUCTURE_: '004A006', SEG_ID: '04081591__,0.12,0.25,65850'}
that looks like this:
['004A006', '04081591__,0.12,0.25,65850']
Whats the easiest way to do that?
Use Object.values to get the object's enumerable property values:
var obj = {STRUCTURE_: '004A006', SEG_ID: '04081591__,0.12,0.25,65850'}
const res = Object.values(obj)
console.log(res)
This question already has answers here:
How to get all key in JSON object (javascript)
(7 answers)
Closed 2 years ago.
I have a varible, if I print it out, I see this output:
Well, if I'd know the number '118', it would be easy, but in the program where I am using it, I don't know it. So is there any mode to get it without knowing that value?
You could use the Object.keys function to retrieve all keys of your object.
The Object.keys returns an array, you can then access the first element of that array like any other array.
const JSONString = '{"118": {"input1": 6, "input2": 1, "input3": 3}}';
const json = JSON.parse(JSONString);
const keys = Object.keys(json);
console.log(keys[0]);
This question already has answers here:
How to create an object from an Array of key-value pairs?
(7 answers)
Closed 2 years ago.
I would like to create a JS object made of specific array values. For example, I have an array ["bananas", 5] and I would like to convert it into an object {"bananas" : 5}. Could you please let me know how could I do that?
Object.fromEntries can be used to create an object from an array of arrays:
let input = ["bananas", 5];
let output = Object.fromEntries([input]);
console.log(output);
Where each two-elements array represents a key-value pair from your new object.
This question already has answers here:
One liner to delete multiple object properties
(3 answers)
Closed 3 years ago.
I have an array of keys that can be present as propert on object, if present, I want to remove those properties on it, I know removing a single prop on object like this:
const { 'removedPropOnObj', ...newObj } = obj;
newObj here becomes new object with removed property of removedPropOnObj, i want to do something like that but i have in this case is array of keys like:
['removeKey1','removeKey2','removeKey3']
Help?
You can do that using Object.keys and reduce()
let rmKeys =['removeKey1','removeKey2','removeKey3']
let obj = {
removeKey1:1,
removeKey2:2,
removeKey3:3,
removeKey4:4,
removeKey5:5
}
let newObj = Object.keys(obj).reduce((ac,a) => !rmKeys.includes(a) ?({...ac,[a]:obj[a]}) : ac,{})
console.log(newObj);
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
I want alert object key name and its value. But it is not working.
$(function() {
var james = {first: '1,2,3', second: '4,5,6' }
$('a').click(function(){
alert(james[first])
})
})
You should correctly use either square bracket notation:
alert(james["first"])
or dot notation:
alert(james.first)
to access elements in objects.
Useful reference:
http://www.jibbering.com/faq/faq_notes/square_brackets.html
If you need to display all items in the object use for loop with in keyword:
for (var key in james) {
// key -- for key
// james[key] -- for value
}
you can use foreach to read all property :
for (var key in james) {
alert(key);
}