Adding new key/pair elements into the original object: [duplicate] - javascript

This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 2 years ago.
const obj = {name:'James',age:25};
const address = [{add1:'USA'},{add2:'UK'}]
Expecting : obj={name:'James',age:25,tag:[{add1:'USA'},{add2:'UK'}]};
Tried does not work for me
Object.entries(address).forEach(([key,value]) => { obj[key] = value })

let obj = {name:'James',age:25};
let address = [{add1:'USA'},{add2:'UK'}];
obj['tag'] = [...address];
const resultObj = obj;
console.log(resultObj);

Related

Sort JavaScript object by key like this {"ADF_1":"","ADF_2":"","ADF_3":"","ADF_4":""} [duplicate]

This question already has answers here:
Does JavaScript guarantee object property order?
(13 answers)
Closed 6 days ago.
const data = {"ADF_3":"","ADF_2":"","ADF_1":"","ADF_7":"","ADF_17":"","ADF_6":"","ADF_18":"","ADF_5":"","ADF_15":"","ADF_4":"","ADF_16":"","ADF_9":"","ADF_19":"","ADF_8":"","ADF_20":"","ADF_10":"","ADF_13":"","ADF_14":"","ADF_11":"","ADF_12":""};
{"ADF_1":"","ADF_2":"","ADF_3":"","ADF_4":"","ADF_5":"","ADF_6":"","ADF_7":"","ADF_8":"","ADF_9":"","ADF_10":"","ADF_11":"","ADF12":"","ADF_13":"","ADF_14":"","ADF_15":"","ADF_16":"","ADF_17":"","ADF_18":"","ADF_19":"","ADF_20":""};
const data = {"ADF_3":"","ADF_2":"","ADF_1":"","ADF_7":"","ADF_17":"","ADF_6":"","ADF_18":"","ADF_5":"","ADF_15":"","ADF_4":"","ADF_16":"","ADF_9":"","ADF_19":"","ADF_8":"","ADF_20":"","ADF_10":"","ADF_13":"","ADF_14":"","ADF_11":"","ADF_12":""};
console.log(Object.fromEntries(Object.entries(data).sort(([a],[b])=>
a.localeCompare(b,undefined,{numeric: true}))))
const data = {"ADF_3":"","ADF_2":"","ADF_1":"","ADF_7":"","ADF_17":"","ADF_6":"","ADF_18":"","ADF_5":"","ADF_15":"","ADF_4":"","ADF_16":"","ADF_9":"","ADF_19":"","ADF_8":"","ADF_20":"","ADF_10":"","ADF_13":"","ADF_14":"","ADF_11":"","ADF_12":""};
const sortedData = {};
Object.keys(data).sort((a, b) => parseInt(a.replace("ADF_", "")) - parseInt(b.replace("ADF_", "")))
.forEach(key => sortedData[key] = data[key]);
console.log(sortedData);

How change the name of property in array of objects good way? [duplicate]

This question already has answers here:
Changing the key name in an array of objects?
(11 answers)
Rename Object Key in an array using javascript
(6 answers)
How to rename key of object in an array
(4 answers)
Closed 1 year ago.
Now I have object like this
let arr = [{name:'first'},{name:'first1'}]
Exprected
[{value:'first'},{value:'first1'}]
My attempt
let result = arr.map(el=>{
const {name, ...other} = el;
return {value: el.name, ...other}
})
you can do it that way!
let arr = [{name:'first'},{name:'first1'}] //{value: 'first'}
let result = arr.reduce((acc,el)=>{
return [...acc, {'value' : el.name}]
},[])
You can transform the data using Array.prototype.map.
let arr = [{name:'first'},{name:'first1'}]
let result = arr.map(x => {return {value: x.name}});
console.log(result);

How transform string to recursive object? [duplicate]

This question already has answers here:
How to set object property (of object property of..) given its string name in JavaScript?
(16 answers)
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 1 year ago.
I am passing a dot separated string into function
console.log( namespace('a.b.c.d.e'))
and expect get next result
//result => "{"a":{"b":{"c":{"d":{"e":{}}}}}}"
my try (I don't know how to do it recursively)
const namespace = (string)=> {
return string.split('.').reduce((acc,el)=>{
acc[el] = {}
},{})
}
const input = "a.b.c.d.e"
const output = input.split('.').reverse().reduce((acc,el)=>{
return {[el]: acc}
},{})
console.log(output)
How about the below iteration approach :-
function namespace(input){
let result = {};
let temp = result;
const inputArr = input.split(".");
inputArr.forEach((ele)=>{
temp[ele] = {};
temp = temp[ele];
})
return result;
}
console.log( namespace('a.b.c.d.e'))

How to make name of an object key the value of a variable? [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
var orderName = document.getElementById("orderName").value;
var order = ["5", "6"];
var Orders: {
orderName: order
}
JSON.Stringify(Orders); // returns {"ordername":[5,6]}
//Expected output would be {"Hamburger": [5,6]}
How can I make it so when i call JSON.Stringify(Orders) it returns the value of the element?
Use the square bracket property notation:
var orderName = "Hamburger";
var order = ["5","6"];
var obj = { [orderName]: order };
console.log(obj);

Remove object property based on array values [duplicate]

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

Categories