仅当值不为空时才推送 Javascript 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38837341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript array push only if value not null
提问by user5638730
I'm wondering if, and if how the following thing is working:
我想知道以下事情是否以及如何工作:
I have an array defined like the following:
我有一个如下定义的数组:
var array = [
{number: '1', value: 'one', context: 'someContext'},
{number: '2', value: 'two', context: 'anotherContext'},
...
]
What I'm currently doing is pushing the elements into the array, so array.push({number: '1', value: 'one', context: 'someContext'});
and so on, with every array element.
我目前正在做的是将元素推入数组,等等array.push({number: '1', value: 'one', context: 'someContext'});
,每个数组元素。
Now this thing is extended: Say there's another key called 'content'. This key has a appropriate value, that is either undefined or a string. Now the question is: If I put the pushing in a function like this:
现在这件事被扩展了:假设还有另一个名为“内容”的键。该键具有适当的值,即未定义或字符串。现在的问题是:如果我把推入这样的函数:
push(number, value, context, content) {
array.push({
number: number,
value: value,
context: context,
content: content
})
}
Is there anyway, I can make sure, that the key content is only added to the element, if the content (the function gets as parameter) is not null.
无论如何,我可以确保关键内容仅添加到元素中,如果内容(函数作为参数获取)不为空。
Of course I can modify function like that:
当然我可以这样修改函数:
push(number, value, context, content) {
if(!content) {
array.push({
number: number,
value: value,
context: context,
content: content
})
} else {
array.push({
number: number,
value: value,
context: context
})
}
}
But the question is, if there is anyway to do this in the push function. I also thought about something like
但问题是,在push函数中是否有办法做到这一点。我也想过类似的事情
array.push({
number: number,
value: value,
context: context,
content? content: content
})
So it would only be inserted if content is defined, but does this work, it didn't seem like, but maybe theres a mistake in my code.
所以只有在定义了内容时才会插入它,但是这是否有效,它看起来不像,但也许我的代码中有错误。
回答by adeneo
If the objective isn't just to make code shorter, the most readable would be something like this, where you create the object, add the property if there is a value, and then push the object to the array.
如果目标不仅仅是使代码更短,那么最易读的应该是这样的,您可以在其中创建对象,如果有值则添加属性,然后将对象推送到数组。
push(number, value, context, content) {
var o = {
number : number,
value : value,
context : context
}
if (content !== null) o.content = content;
array.push(o);
);
Here's an ES6 way to construct the object directly inside Array.push
, and filter any that has null
as a value.
这是直接在 内部构造对象Array.push
并过滤任何具有null
值的 ES6 方法。
function push(...arg) {
array.push(['number','value','context','content'].reduce((a,b,i)=> {
if (arg[i] !== null) a[b]=arg[i]; return a;
}, {}))
}
回答by Pavlo
If you are open to using ES2015, this can be done with Object.assign
:
如果您愿意使用 ES2015,可以通过以下方式完成Object.assign
:
array.push(
Object.assign(
{ number, value, context },
content ? { content } : null
)
);
回答by dhilt
With Spread operatorfor object literals (ECMAScript 2018) it looks super easy:
使用对象字面量的扩展运算符(ECMAScript 2018) 看起来非常简单:
const myPush = (number, value, context, content) =>
array.push({
...{ number, value, context },
...content && { content }
});
回答by Maciej Sikora
It can be done by extending array:
它可以通过扩展数组来完成:
//create Your own array object
myArray=function(){};
myArray.prototype=Object.create(Array.prototype);
//create method
myArray.prototype.pushDefined=function(obj){
var newObj={};//create clean object
for (var key in obj){
if (typeof obj[key]!='undefined' && obj[key]!=null){
//this element is defined and is not null
newObj[key]=obj[key];
}
}
this.push(newObj);//push clean object without undefind properties
};
//tests
var arr=new myArray();
arr.pushDefined({ name:"John",surname:null});
console.log(arr[0]);
Or add this method to Array prototype:
或者将此方法添加到 Array 原型中:
Array.prototype.pushDefined=function(obj)... //this will be method in every array
I would not recommend changing original push method in Array because always think about other programmers who are using Array in this particular project.
我不建议更改 Array 中原始的 push 方法,因为要始终考虑在此特定项目中使用 Array 的其他程序员。