i have this object
{
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier': '918312asdasc812',
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name': 'Login1',
'http://schemas.microsoft.com/ws/2008/06/identity/claims/role': 'User'
}
How read value for key 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier'?
Solution:
obj['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier']
You can put the values of the object in an array and access the array:
const object1 = {
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier': '918312asdasc812',
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name': 'Login1',
'http://schemas.microsoft.com/ws/2008/06/identity/claims/role': 'User'
};
const arr = (Object.values(object1));
console.log(arr[0]);
to access key, value pairs in an object you can use Object.entries(yourObjName).
foo = {
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier': '918312asdasc812',
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name': 'Login1',
'http://schemas.microsoft.com/ws/2008/06/identity/claims/role': 'User'
}
for (const [key, value] of Object.entries(foo)) {
console.log(`${key}: ${value}`);
}
read more information on Object.entries()
Thanks #Andy
Solution:
obj['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier']
Related
I want to convert object1 to object2 dynamically because keys like apple and water and inside objects are not static.
const object1 = {
apple:[
{a:''},
{b:''}
],
water:[
{c:''},
{d:''}
]
}
convert to this form:
object2 = {
apple:{a:'',b:''},
water:{c:'',d:''}
}
Use Object.entries to iterate the key value pairs, then use Object.assign to merge the inner objects, and finally collect the generated pairs back into one object with Object.fromEntries:
const object1 = {apple:[{a:''},{b:''}],water:[{c:''},{d:''}]}
const object2 = Object.fromEntries(
Object.entries(object1).map(([key, arr]) =>
[key, Object.assign({}, ...arr)]
)
);
console.log(object2);
const object1 = {
apple:[
{a:''},
{b:''}
],
water:[
{c:''},
{d:''}
]
}
let object={}
Object.keys(object1).forEach((item)=>{
let obj={};
object1[item].map((e)=>{
obj={...obj,...e};
});
object[item]=obj;
})
console.log(object)
Basically I have an enum describing status
status = {1: "new", 2: "working" ... }
and I need to take it from that to something like
status = [{1: "new"}, {2: "working"} ...]
in a clean way ... i.e.
something readable, (maybe) simple and reliable. Currently I am not sure how to go about this ... maybe go through the object and "map" each pair to a brand new object in an array? Sounds bad.
Object.entries(data).map(([key, value]) => ({[key]: value}))
The result of the map is the array, what you want.
Or:
const arrayData = [];
for (const key in data) {
arrayData.push({[key]: data[key]})
}
iterate on objects dynamicly:
const object1 = {
a: 'somestring',
b: 42
};
for (const [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
const { object } = require("prop-types")
const status = { 1: "new", 2: "working" }
const statusUpdate = Object.entries(status).map(([key, value]) => ({ [key]: value }))
console.log(statusUpdate);
This question already has answers here:
Remove blank attributes from an Object in Javascript
(53 answers)
Closed 2 years ago.
Is there a better way to accomplish this in Javascript?
const data: any = {};
if (values.email) data.email = values.email;
if (values.password) data.password = values.password;
if (values.username) data.username = values.username;
I don't want the data object to have the properties for the undefined or falsy values.
You could put the potential properties in an array and then .filter() out any which values[prop] has a fasly value for. Then you can .map() each key to an array of [key, value] pairs, and use Object.fromEntries() to build the data object for you:
const values = {
email: 'abc',
username: 'xyz'
};
const props = ['email', 'password', 'username'];
const data = Object.fromEntries(
props.filter(prop => values[prop]).map(prop => [prop, values[prop]])
);
console.log(data);
If you can't support Object.fromEntries(), you could consider using Object.assign(), and then mapping to an array of objects which you then spread into Object.assign():
const values = {
email: 'abc',
username: 'xyz'
};
const props = ['email', 'password', 'username'];
const data = Object.assign({},
...props.filter(prop => values[prop]).map(prop => ({[prop]: values[prop]}))
);
console.log(data);
You can do something along these lines:
const obj1 = {
prop1: true,
prop2: false,
prop3: 4,
}
const obj2 = Object.entries(obj1).reduce((result, [key, value]) => {
if (value) {
result[key] = value
}
return result
}, {})
console.log(obj2)
This simple function will do it, if you want to copy over all properties that don't have a false-y value. If you only want a fixed list, look at the answer from Nick Parsons.
const copyNonNil = (obj) =>
Object .fromEntries (Object .entries (obj) .filter(([k, v]) => v))
const values = {
email: 'foo#bar.baz',
username: 'foobar',
password: '',
another: false
}
console .log (copyNonNil (values))
It's easy to shim Object .fromEntries if you don't have it available. See the answer from theDude, which uses a reduce that serves in the same role.
You could use Object.entries to iterate over the properties of values and perform the true/false check in that to make this more flexible:
for(const [key, value] of Object.entries(values)){
if(values[key]) data[key] = value;
}
This would give you only the truthy values in data for a values object of any size.
I have an object like
let obj = {
1: true,
2:false,
3:true
}
How can i return the object key wherever the object value is false, for example, in the case above, only 2 should be returned
I tried Object.values(obj).filter(value => !value) but it only returns false
You are using filter() on Object.values() this way you will not have any way to access the corresponding key
You can use filter() on Object.keys() and check if value at that key is truthy.
let obj = {
1: true,
2:false,
3:true
}
const res = Object.keys(obj).filter(k => !obj[k]);
console.log(res)
Actually, came up with
Object.entries(obj).filter(([key, value]) => !value).map(([key, value]) => key);
which returns what I need.
I need to add single quotes to all the values in an object that looks like this:
{name: "testUser", type: "regular"}
The object needs to look like the following:
{name: "'testUser'", type: "'regular'"}
I'm using Object.values to achieve this:
Object.values(targetObject).map(value => value = `'${value}'`)
Except it's not exactly updating targetObject. What would be the correct way to achieve this?
To update the original object, you'll need the key. Use Object.entries() to get the key and value, iterate with Array.forEach(), and each key with the new value:
const targetObject = {name: "testUser", type: "regular"}
Object.entries(targetObject) // get an array of [key, value] pairs
.forEach(([k, v]) => targetObject[k] = `'${v}'`) // update the original object keys
console.log(targetObject)
You can use Object.entries to iterate through all the entries in the object and update them:
const obj = {name: "testUser", type: "regular"};
for (let [key, value] of Object.entries(obj)) {
obj[key] = `'${value}'`;
}
console.log(obj);
You can you for..in to loop through properties of the object.
const obj = {name: "testUser", type: "regular"}
for (let k in obj){
obj[k] = `'${obj[k]}'`
}
console.log(obj)
You could map new objects and collect them to a single object.
var object = { name: "testUser", type: "regular" },
updated = Object.assign(...Object.entries(object).map(([k, v]) => ({ [k]: `'${v}'` })));
console.log(updated);