我正在使用Object.assign()
将值复制到对象:
const { one, two, three } = attributes;
return Object.assign( props, {
"data-one": one,
"data-two": two,
"data-three": three
} );
如果属性的值为空,则返回undefined。我希望它返回一个空字符串,或者理想情况下不复制任何空属性。
我试过了:
const { one, two, three } = attributes;
var oneValue = one ? one : "";
var twoValue = two ? two : "";
var threeValue = three ? three : "";
return Object.assign( props, {
"data-one": oneValue,
"data-two": twoValue,
"data-three": threeValue
} );
它的工作原理是发送空值而不是未定义,但似乎不是很花哨。有没有更好的方法来解决这个问题?
答案 0 :(得分:6)
您可以在使用解构时定义默认值。
const attributes = { one: 'one' };
const { one = '', two = '', three = '' } = attributes;
const props = {}
console.log(Object.assign( props, {
"data-one": one,
"data-two": two,
"data-three": three
}));
最后,如果你有很多共同的行为,你可以命名你要复制的属性并写一个循环。
答案 1 :(得分:1)
@CyberJunkie ,您可以使用对象销毁的概念进行修复,如下所示:
注意:访问Destructuring assignment和Destructuring assignment - Mozilla.org's documentation,了解销毁对象。
function getObj(attributes)
{
const { one: newOne = '', two: newTwo = '', three: newThree = '' } = attributes;
// var oneValue = one ? one : "";
// var twoValue = two ? two : "";
// var threeValue = three ? three : "";
var props = {four: "The four", six: "The six"};
return Object.assign( props, {
"data-one": newOne,
"data-two": newTwo,
"data-three": newThree
} );
}
var prettyObj = JSON.stringify(getObj({one: "ObjData1.1", two: 'ObjData2', three: 'ObjData3'}), null, 4);
console.log(prettyObj)
/*
{
"four": "The four",
"six": "The six",
"data-one": "ObjData1.1",
"data-two": "ObjData2",
"data-three": "ObjData3"
}
*/
prettyObj = JSON.stringify(getObj({one: "ObjData1.2", two: 'ObjData2'}), null, 4);
console.log(prettyObj)
/*
{
"four": "The four",
"six": "The six",
"data-one": "ObjData1.2",
"data-two": "ObjData2",
"data-three": ""
}
*/
prettyObj = JSON.stringify(getObj({one: "ObjData1.2"}), null, 4)
console.log(prettyObj)
/*
{
"four": "The four",
"six": "The six",
"data-one": "ObjData1.2",
"data-two": "",
"data-three": ""
}
*/
最后,您可以查看我在 NODE REPL 上执行的代码,因为它显示了const { one: newOne = '', two: newTwo = '', three: newThree = '' } = attributes;
和const { one = '', two = '', three = '' } = attributes;
语句之间的差异。
H:\RishikeshAgrawani\Projects\Stk\UndefinedValue>node
>
> // Example 1
undefined
>
> attributes = {one: "ObjData1.2", two: 'ObjData2'}
{ one: 'ObjData1.2', two: 'ObjData2' }
>
> const { one: newOne = '', two: newTwo = '', three: newThree = '' } = attributes;
undefined
>
> one
ReferenceError: one is not defined
>
> newOne
'ObjData1.2'
>
> newTwo
'ObjData2'
>
> newThree
''
>
> // Example 2
undefined
> attributes2 = {one: "ObjData1.2", two: 'ObjData2'}
{ one: 'ObjData1.2', two: 'ObjData2' }
>
> const { one = '', two = '', three = '' } = attributes;
undefined
> one
'ObjData1.2'
> two
'ObjData2'
> three
''
>
感谢。
答案 2 :(得分:-1)
function assignNotUndefined(target, obj) {
for(const key of Object.keys(obj)) {
if(obj[key] !== undefined)
target[key] = obj[key];
}
return target;
}
可用作:
const { one, two, three } = attributes;
return assignNotUndefined( props, {
"data-one": one,
"data-two": two,
"data-three": three
});