How to create new json object from exiting one? [duplicate] - javascript

This question already has answers here:
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
Closed 7 years ago.
I want to create new json object from exiting one, because if I create from
var person1= { First Name: 'John',
Last Name: 'Smith',
age: 54,
dateofbirth: '31-08-68',
}
var person2=person1;
delete person2.age
This will also delete age from the person1, I don't want to delete age from person1, but person2. Because I need to initialize other person also. So How I create new json object from exiting object .

You must clone the object. The quick way is
var person2=JSON.parse(JSON.stringify(person1));

Related

Working with property accessors using string [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
How can I refer to a variable using a string containing its name?
(7 answers)
Is there a way to access a javascript variable using a string that contains the name of the variable?
(4 answers)
Closed 9 months ago.
I am trying to access a property’s value in an object, I know the name of the properties, but not the name of the object (if that makes sense).
This object name is retrieved from an array. Sorry I’m not very good with explaining things, but hopefully an example will help…
So for example…
const names = ["john", "ben", "doe"]
const john = {
age: 33,
hobby: "coding"
};
const ben = {
age: 25,
hobby: "soccer"
};
const doe = {
age: 20,
hobby: "playing games"
};
names.forEach((name) => {
console.log(name.age)
console.log(name.hobby)
});
So as you can see, I know what properties each object has, the object name I also know, but it is being passed in an array of strings.
When I run this code, it will log undefined, how would I be able to retrieve the age and hobby of each of the values in the array?

Javascript: change object key and value [duplicate]

This question already has answers here:
Changing the key name in an array of objects?
(11 answers)
Closed 2 years ago.
I have an object that I receive from an api, and each object's length are similarly long.
now, it does have a key fname and lname and I want it to become just name. I don't want to manually create an empty array and push a new one there just to change fname and lname into name.
Is there anyway I can achieve this without looping through the object and manually pushing it?
You can destructure the object while mapping it, here I have created a dummy data, let me know if this is something what you need:
var array=[{id:1, fname:'Bob', lname: 'Alice', someotherkey:'key'}];
var result = array.map(({fname, lname, ...rest})=>({name:fname+' '+lname, ...rest}));
console.log(result);

Find specific item in an array? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
I have an array that looks like this:
cardNumber: "2344234454562036"
cardType: "Visa"
cardholderName: "Yeyy"
cvv: "123"
expiryMonth: 12
expiryYear: 2022
postalCode: "52636"
redactedCardNumber: "••••••••••••2036"
__proto__: —'
I need to get the value of redactedCardNumber from that array.
The first thing that I am confused about is that when i print the above 'array' in the console, it actually show an object before everything! so is this an array or an object?
also, what is the best way of getting the specific item like 'redactedCardNumber' from it?
It looks like an object. If so, you can access it by obj.redactedCardNumber.

How to print all the properties of the object in javascript? [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 4 years ago.
var object = { name: 'Harry', age: '25', sex: 'male'...... n};
This object has 'n' number of properties which I don't know and I would like to print these whole properties.
There are heaps of solutions from a quick Google, a recomended result is; Print content of JavaScript object?
console.log(JSON.stringify(object, null, 4));
The second argument alters the contents of the string before returning it. The third argument specifies how many spaces to use as white space for readability.
You can use the The Object.keys() function to get an array of the properties of the object:
var obj = { name: 'Harry', age: '25', sex: 'male'};
Object.keys(obj).forEach((prop)=> console.log(prop));

Translating literal object into object's properties [duplicate]

This question already has answers here:
How to duplicate object properties in another object? [duplicate]
(16 answers)
Closed 8 years ago.
Given this object:
var myObject = {name: 'David', date: '11/13/2014'};
I want my constructor to set its object properties based on myObject:
function myClass(_object){
// set given object's properties here
}
I want to set the properties through a loop, so I wont have to set every property by hand - because the object will be dynamic.
I'm looking for a cross-browser solution.
Perhaps what you want is this:
function MyClass(_object) {
for (var p in _object) {
if (_object.hasOwnProperty(p)) {
this[p] = _object[p];
}
}
}
Don't forget to use new:
var obj = new MyClass(myObject);
Fiddle

Categories