How to access object [duplicate] - javascript

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);
}

Related

Replace the value in the object with an unknown key with another value from the second object [duplicate]

This question already has answers here:
How to get a key in a JavaScript object by its value?
(31 answers)
Find key for specific value on object in JS
(5 answers)
Dynamically get 'Key' with 'Value' in javascript
(3 answers)
Closed 1 year ago.
I have an objects:
http ={" xxx": "#phone#","yyy": "1234", "zzz":5678 }
input= {"phone": "2", "id": "258 },
How do I find the #phone# value and replace it with 2 from input?
The #phone# key can be anything, not just "xxx".
You could use Object.entries() to iterate over each property of the object and then check with an if statement if the key-value is the one that you want and change it.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
Use Object.entries() to loop over the properties of the object and then check with an if statement whether the value is the one that you want and change it to the desired value.
const entries = Object.entries(id);
entries.forEach((entry) => {
if (entries[1] === '#phone#') {
id[entry[0]] = 'SOME_VALUE';
}
});

Loop through javascript object without knowing names of items [duplicate]

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]}`)
}

remove property on object if its' key is present on array that contains keys? [duplicate]

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);

get value with string key in javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 9 years ago.
I can't figure out how to get an object property using a string representation of that property's name in javascript. For example, in the following script:
consts = {'key' : 'value'}
var stringKey = 'key';
alert(consts.???);
How would I use stringKey to get the value value to show in the alert?
Use the square bracket notation []
var something = consts[stringKey];
Javascript objects are like simple HashMaps:
var consts = {};
consts['key'] = "value";
if('key' in consts) { // true
alert(consts['key']); // >> value
}
See: How is a JavaScript hash map implemented?

How to access object using dynamic key? [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 9 years ago.
How to access an object using a variable as key. Here is my code sample:
var o = {"k1": "111", "k2": "222"};
alert(o.k1); //working fine
var key = "k"+1; alert(key); // k1
alert(o.key); //not working
You can access objects like arrays:
alert(o[key]);
Change the last line to: alert(o['k1']); or alert(o[key]); where key is your dynamically constructed property key.
Remember you can access object's properties with array notation.
Consider using a for...in loop

Categories

Resources