This question already has answers here:
How to determine whether an object has a given property in JavaScript
(12 answers)
Closed 6 years ago.
I have my reference array
const reference = ['prefix', 'suffix', 'student_email']
and my object that looks like this
const obj = {
'prefix':'John',
'suffix':'Doe',
'student_email':'johndoe23#rigly.org',
'course_code':'PJ4004',
'professor':'McMillian'
}
I'd like to remove 'course_code' & 'professor' as its not apart of the reference array. How can I do that?
Expected output:
const obj = {
'prefix':'John',
'suffix':'Doe',
'student_email':'johndoe23#rigly.org',
}
What I have:
reference.map(v => {
delete obj[v]; // this will delete what I don't want it to delete
});
How can I only remove those I don't need/ aren't present in the reference array?
You can loop through Object#keys and delete the properties not found in the array:
const reference = ['prefix', 'suffix', 'student_email']
const obj = {
'prefix':'John',
'suffix':'Doe',
'student_email':'johndoe23#rigly.org',
'course_code':'PJ4004',
'professor':'McMillian'
}
Object.keys(obj).forEach(i=>{
if(reference.indexOf(i) === -1) delete obj[i];
});
console.log(obj);
Related
This question already has answers here:
How do you update an object in an array based on another array?
(3 answers)
Closed 3 months ago.
I have javascript object like this
var arr1 = [{name:'qqq'},
{name:'www'},
{name:'eee'},
{name:'rrr'}]
var arr2 = [{value:'qqq',url:'123'},
{value:'www',url:'456'}]
I need to replace objects in arr1 with url from arr2 with matching name and value.
So here is the result I want to get:
var arr1 = [{name:'123'},
{name:'456'},
{name:'eee'},
{name:'rrr'}]
So you need to loop through arr1 and filter arr2 by name value, if you will get result, then replace name value with url value:
let replacedValues = arr1.map(o1 => {
let fResult = arr2.filter(o2 => o2.value === o1.name)[0]
if(fResult) {
o1.name = fResult.url
}
return o1
})
This question already has answers here:
Get the index of the object inside an array, matching a condition
(16 answers)
Closed 2 years ago.
I have an array of object that holds two objects. Each object has a couple properties; one being dealerNo. Given the value of dealerNo, how would I get the index of that object?
Use .findIndex:
const getIndexByDealerNo = (arr=[], dealerNo) =>
arr.findIndex(e => e.dealerNo===dealerNo);
const arr = [ { dealerNo:1 }, { dealerNo: 2 } ];
console.log( getIndexByDealerNo(arr, 1) );
console.log( getIndexByDealerNo(arr, 2) );
console.log( getIndexByDealerNo(arr, 11) );
This question already has answers here:
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 2 years ago.
Here i am having two different objects :
let oldObject = {name:"Dhanush",age:24,sex:"Male",education:"Btech"}
let newObject = {name:"Dhanush kumar S",age:23,sex:"Male"}
result should be comparing this above two objects and check if the key are same , then update the oldObject value with newObject . The result has to be like this
let updatedObject = {name:"Dhanush kumar S",age:23,sex:"Male",education:"Btech"}
I tried by doing something like this, but this doesnt help. Your help is much appreciated.
const compareObjects = () => {
for (let [key,value] in oldObject) {
if (newObject.hasOwnProperty(key)) {
oldObject[newObject[key]] = newObject[value]
delete oldObject[key]; //remove old entry
}
}
console.log(oldObject)
}
compareObjects()
You can do this by using the spread syntax.
Just spread the old object first followed by the new object.
Matching keys if any would be updated by the values from the new object and new keys in the new object would be added:
let oldObject = {name:"Dhanush",age:24,sex:"Male",education:"Btech"}
let newObject = {name:"Dhanush kumar S",age:23,sex:"Male"}
const merge = (oldObj, newObj) => {
return {...oldObj, ...newObj};
}
console.log(merge(oldObject, newObject));
This question already has answers here:
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
Closed 2 years ago.
let promiseArr = [];
for (let i = 0; i < ISOarr.length; i++) {
options.body.start_time = ISOarr[i];
let newOptions = cloneOptions(options);
promiseArr.push(newOptions);
}
console.log(promiseArr);
Returns an array of the same object.
Clone method:
cloneOptions = options => {
let clone = {};
for( let key in options ) {
clone[key] = options[key]
}
return clone;
}
So my question is how do I push an object that is not the same reference as the previous objects pushed, because even with it supposedly creating a new clone each loop it still somehow creates the same referenced object.
In the loop if I console.log I get the proper output with the value of the key changed, but once it's pushed into the array and we console.log the array, all objects are the same. Any suggestions would be super helpful. Thank you in advance!
Can you try this
cloneOptions = options => {
return JSON.parse(JSON.stringify(options))
}
This question already has answers here:
How to transpose a javascript object into a key/value array
(11 answers)
Closed 5 years ago.
I want to convert an object of key values to an array of objects in javascript
var obj={"name1":"value1","name2":"value2",...};
How can i convert it to
arr=[{"name":"name1","value":"value1"},{"name":"name2","value":"value2"},...];
Try with array#map and Array#push
var obj={"name1":"value1","name2":"value2"};
var res=[];
Object.keys(obj).map(a => res.push({name:a , value:obj[a]}))
console.log(res)
Short answer (ES6):
const arr = Object.entries(obj).map(([name, value]) => {
return {
name,
value
}
});
Another answer (ES5):
var arr = Object.keys(obj).map(function(key) {
return {
name: key,
value: obj[key]
}
});