I have this object
myObject = {
id: "44",
name: "name",
firstName: "fn",
lastName: "tt",
cars: [],
houses: [],
family: {}
}
I want to format this object to
myObject = {
person: {
id: "44",
name: "name",
firstName: "fn",
lastName: "tt"
},
cars: [],
houses: [],
family: {}
}
Is there anyway i can do this without using delete?
You can use destructuring:
const myObject = { id:"44", name:"name", firstName:"fn", lastName:"tt", cars: [], houses:[], family:{}}
const { houses, cars, family, ...rest } = myObject
const myNewObject = {
person: rest,
houses,
cars,
family
}
console.log(myNewObject)
You can use destructuring assignments to decompose your object into variables and restructure them in a simple function.
const format = (obj) => {
const {id, name, firstName, lastName, ...props} = obj;
return {person: {id, name, firstName, lastName}, ...props}
}
const formatted = format(myObject);
console.log (formatted)
<script>
var myObject = { id:"44", name:"name", firstName:"fn", lastName:"tt", cars: [], houses:[], family:{}}
</script>
You could create a new object and assign the properties to it:
let obj = { id:"44", name:"name", firstName:"fn", lastName:"tt", cars: [], houses:[], family:{}}
//I want to format this object to
//myObject = { person: {id:"44", name:"name", firstName:"fn", lastName:"tt"}, cars: [], houses:[], family:{}}
let res = {}
res.person = {id: obj.id, name: obj.name, firstName: obj.firstName, lastName: obj.lastName}
res.cars = obj.cars
res.houses = obj.houses
res.family = obj.family
console.log(res)
You can destructure what you need, put the rest into a variable rest, and then re-assign myObject:
let myObject = {
id: "44",
name: "name",
firstName: "fn",
lastName: "tt",
cars: [],
houses: [],
family: {}
}
const {id, name, firstName, lastName, ...rest } = myObject;
myObject = {
person: { id, name, firstName, lastName },
...rest
}
console.log(myObject);
Related
I want to write unit testing using Jest for my Node.js functions and need to test if response object has some specific keys or not. Something like this:
expect(await mokChannelsService.getChannel(inputDto)).toEqualKeys(outputDto);
// Or just a normal function
const result = await mokChannelsService.getChannel(inputDto);
const equal = isEqualKeys(result, outputDto);
This toEqualKeys function should check equality just for keys not values. And these objects are not instances of a class. for example these two objects should be equal:
const object1 = {
names: {
firstName: '',
lastName: '',
},
phone: 1,
};
const object2 = {
names: {
firstName: 'John',
lastName: 'Due',
},
phone: 1234567890,
};
const object3 = {
names: {
firstName: 'John',
lastName: 12,
},
mobile: '12345',
};
const result1 = toEqualKeys(object1, object2); // true
const result1 = toEqualKeys(object1, object3); // false
Is there any NPM packages available for this?
You can iterate each of the keys in object1, checking that the same key exists in object2 and - if the associated property in object1 is an object, that all the keys from that object also exist in a similar object in object2:
const object1 = {
names: {
firstName: '',
lastName: '',
},
phone: 1,
};
const object2 = {
names: {
firstName: 'John',
lastName: 'Due',
},
phone: 1234567890,
};
const object3 = {
names: {
firstName: 'John',
lastName: 12,
},
mobile: '12345',
};
const toEqualKeys = (obj1, obj2) => Object.keys(obj1)
.every(k => k in obj2 &&
(typeof obj1[k] != 'object' || typeof obj2[k] == 'object' && toEqualKeys(obj1[k], obj2[k]))
)
console.log(toEqualKeys(object1, object2))
console.log(toEqualKeys(object1, object3))
You can get more detail on the differences by logging differences into an array (using reduce to iterate the keys in the reference object). This function will return an array of errors (empty if there are none) for each comparison:
const object1 = {
names: {
firstName: '',
lastName: '',
},
phone: 1,
};
const object2 = {
names: {
firstName: 'John',
lastName: 'Due',
},
phone: 1234567890,
};
const object3 = {
names: {
firstName: 'John',
lastName: 12,
},
mobile: '12345',
};
const object4 = {
names: 'John',
mobile: '12345',
};
const object5 = {
names: {
firstName: 'John',
lastName: 12,
},
phone: {
landline : '12345',
mobile : '555'
}
};
const object6 = {
names: {
firtName: 'John',
lastName: 'Due',
},
phone: 1234567890,
};
const notEqualKeys = (obj1, obj2, prefix = '') => Object.keys(obj1)
.reduce((acc, k) => {
if (!(k in obj2)) acc.push(`key ${k} not present in obj2${prefix}`)
if (typeof obj1[k] == 'object') {
if (typeof obj2[k] == 'object') {
acc = acc.concat(notEqualKeys(obj1[k], obj2[k], `${prefix}.${k}`))
}
else {
acc.push(`${k} is an object in obj1${prefix} but a value in obj2${prefix}`)
}
}
else if (typeof obj2[k] == 'object') {
acc.push(`${k} is a value in obj1${prefix} but an object in obj2${prefix}`)
}
return acc
}, [])
console.log((arr = notEqualKeys(object1, object2)).length ? arr : true)
console.log((arr = notEqualKeys(object1, object3)).length ? arr : true)
console.log((arr = notEqualKeys(object1, object4)).length ? arr : true)
console.log((arr = notEqualKeys(object1, object5)).length ? arr : true)
console.log((arr = notEqualKeys(object1, object6)).length ? arr : true)
I am trying to get the values from an array and assign to an array of objects as a new property.
The array of objects:
const employees = [
{
firstName: "Ana",
lastName: "Rosy"
},
{
firstName: "Zion",
lastName: "Albert"
},
{
firstName: "John",
lastName: "Doe"
}
];
An array
const ages = [30, 60, 45]
Desired result
const employees = [
{
firstName: "Ana",
lastName: "Rosy",
age:30
},
{
firstName: "Zion",
lastName: "Albert",
age:60
},
{
firstName: "John",
lastName: "Doe",
age:45
}
];
I tried something like this
const employeesWithAge = (ages) => {
return employees.map((row) => {
return {
...row,
age: ages
};
});
};
const result = employeesWithAge(ages);
console.log("result", result);
But it adds the entire array of ages to the employees array of objects.
Any help will be appreciated
You can use Array.prototype.map() for this. Use the spread syntax (...) on the current element to copy the properties into a new object, then add the age property based on the ages array, using .map() callback's second parameter (the current index).
employees.map((employee, i) => ({...employee, age: ages[i]}));
Live example:
const employees = [{
firstName: "Ana",
lastName: "Rosy"
},
{
firstName: "Zion",
lastName: "Albert"
},
{
firstName: "John",
lastName: "Doe"
}
];
const ages = [30, 60, 45];
const result = employees.map((employee, i) => ({ ...employee, age: ages[i] }));
console.log(result);
I think a simple for loop should solve your problem.
for (let i = 0; i < employees.length; i++) {
employees[i]["age"] = ages[i];
}
This loop iterates through the length of your list of employee objects and adds a new "age" attribute to each object with the corresponding age in your ages array.
All together I imagine your code looking something like this.
const employees = [
{
firstName: "Ana",
lastName: "Rosy"
},
{
firstName: "Zion",
lastName: "Albert"
},
{
firstName: "John",
lastName: "Doe"
}
];
const ages = [30, 60, 45];
for (let i = 0; i < employees.length; i++) {
employees[i]["age"] = ages[i];
}
console.log(employees);
Here is two array one is firstName another is lastName
{
firstName: ['name1', 'name2'],
lastName: ['last1', 'last2'],
}
but I want to formate the data into one array like this.
{
"name": [{
"firstName": "name1",
"lastName": "last1"
},
{
"firstName": "name2",
"lastName": "last2"
},
]
}
Using Array#map:
const getNameObj = (data = {}) => {
const { firstName = [], lastName = [] } = data;
if(firstName.length !== lastName.length) return;
const name = firstName.map((fn, i) => (
{ firstName: fn, lastName: lastName[i] }
));
return { name };
};
console.log(
getNameObj({ firstName: [ 'name1', 'name2' ], lastName: [ 'last1', 'last2' ] })
);
You could loop through it using for...in and push each element inside new array like this example
const data = {
firstName: [ 'name1', 'name2' ],
lastName: [ 'last1', 'last2' ],
}
let newData = [];
for (let el in data.firstName){
newData.push({
firstName: data.firstName[el],
lastName: data.lastName[el],
})
}
console.log(newData)
You can use Array.reduce to get the result.
const input = {
firstName: [ 'name1', 'name2' ],
lastName: [ 'last1', 'last2' ],
};
const result = input.firstName.reduce((acc, firstName, index) => {
acc.name.push({
firstName: firstName,
lastName: input.lastName[index]
})
return acc
}, {'name': []})
console.log(result);
You can build a new object {name: <value>}, and set your <value> to a mapped version of the firstName array, where each firstName from your array is mapped to an object of the form {firstName: ..., lastName: ...}. Here the firstName key has a value that is the current name, and the lastName key has a value from the lastName array at the corresponding index:
const obj = { firstName: ['name1', 'name2'], lastName: ['last1', 'last2'], };
const res = {
name: obj.firstName.map((firstName, i) => ({firstName, lastName: obj.lastName[i]}))
}
console.log(res);
here it is:
let obj = {firstName: ['name1', 'name2'],lastName: ['last1', 'last2']};
let finalObj = {};
finalObj.name = []
for (let i = 0; i < obj.firstName.length; i++)
finalObj.name.push(
{
firstName: obj.firstName[i],
lastName: obj.lastName[i]
}
)
Within my Angular application, I need to compare two typescript objects and create a new object that consists of key/value pairs where the value of the key is different in the second object.
Here is my code:
const objBefore = {id: 100088, firstName: "Joe", lastName: "Smith", notes: null};
const objAfter = {id: 100088, firstName: "John", lastName: "Johnson", notes: null};
let newObj = {};
for(let key in objBefore) {
if (objBefore[key] !== objAfter[key]) {
let newEntry = { key: objAfter[key]}
Object.assign(newObj, newEntry)
}
}
console.log(newObj)
The output is:
{ key: 'Johnson' }
I need the output to be:
{ firstName: "John", lastName: "Johnson" }
How do I assign the value of the key (e.g., firstName) instead of the variable (key)?
Just use square brackets on [key]
const objBefore = {id: 100088, firstName: "Joe", lastName: "Smith", notes: null};
const objAfter = {id: 100088, firstName: "John", lastName: "Johnson", notes: null};
let newObj = {};
for(let key in objBefore) {
if (objBefore[key] !== objAfter[key]) {
let newEntry = { [key]: objAfter[key]}
Object.assign(newObj, newEntry)
}
}
I created a form to get some info from User, and I want to move some of their info into a nested object. the reason why is to better organize my data later in front-end.
As a simple example, how to create the following "newInfo" out of "oldInfo" in JavaScript?
oldInfo = {
name: 'John',
Age: '32',
friend1: 'Michael',
friend2: 'Peter',
};
newInfo = {
name: 'John',
Age: '32',
friends: {
friend1: 'Michael',
friend2: 'peter',
},
};
I'm sure this must be a repeated and simple topic, but I couldn't find any as I didn't know what keyword to search for.
You could explicitly assign it
const oldInfo = {
name: "John",
Age: "32",
friend1: "Michael",
friend2: "Peter",
}
const newInfo = {
name: oldInfo.name,
Age: oldInfo.Age,
friends: {
friend1: oldInfo.friend1,
friend2: oldInfo.friend2,
},
}
console.log(newInfo)
You can do this easily with spread operator:
const { name, Age, ...friends } = oldInfo;
newInfo = { name, Age, friends };
It simply extracts all fields except name and age as friends.
Example:
const oldInfo = {
name: 'John',
Age: '32',
friend1: 'Michael',
friend2: 'Peter',
};
const { name, Age, ...friends } = oldInfo;
const newInfo = { name, Age, friends };
console.log(newInfo);
If you have a dynamic number of friend: name key-value pairs and other properties that shouldn't be nested into friends then you can use Object.entries and reduce:
const oldInfo = {
name: 'John',
Age: '32',
friend1: 'Michael',
friend2: 'Peter',
};
const newInfo = Object.entries(oldInfo).reduce((acc, [k, v]) => {
if(k.startsWith('friend')) {
acc.friends ? acc.friends[k] = v : acc.friends = {[k]: v};
} else {
acc[k] = v;
}
return acc;
},{});
console.log(newInfo);