Reputation: 4430
I have an array formed by a series of objs, I would like to make sure to assign an object to a variable.
If the object doesn't exist I would like to assign {}
(empty obj).
Can you give me some advice?
let a = [
{
username: 'james.bond',
name: 'James Bond',
email: '[email protected]',
},
{
username: 'sherlock.holmes',
name: 'Sherlock Holmes',
email: '[email protected]',
},
{
name: 'Shinichi Kudo',
email: '[email protected]',
badgeText: '21',
badgeColor: '#fff',
badgeBackground: '#25dbd2',
joined: 'Joined at Jun 31, 2021',
circle: ['transparent', 'transparent'],
},
{
name: 'Arthur Conan Doyle',
email: '[email protected]',
circle: ['transparent', 'transparent'],
},
];
const [b = {}, c = [] ] = a;
console.log(b, c);//b=a[0], c=[a[1],a[2],a[3]]
Upvotes: 0
Views: 55
Reputation: 2526
Try this:
const [ b = {}, c = {}, d = {} ] = a;
Or:
const [ b = {}, ...c] = a;
c
will be an array contains the rest of a
except the first element.
Upvotes: 2
Reputation: 1
try this:
let a = [{
username: 'james.bond',
name: 'James Bond',
email: '[email protected]',
},
{
username: 'sherlock.holmes',
name: 'Sherlock Holmes',
email: '[email protected]',
}
];
const [b = {}, c = {}, d = {} ] = a;//a.map(x => x)
console.log(b, c, d);
Upvotes: 0