我有一个数组如下
[ 'test123', '', 'testabc' ]
如何根据此数组的数组索引创建数组。我想要的输出为[ '1', '0','1']
。如果索引中有任何元素,则应该给出1,否则为0.在nodejs中生成这样的数组的最佳方法是什么。
答案 0 :(得分:1)
您可以使用array#map
var data = [ 'test123', '', 'testabc' ],
result = data.map(v => v? '1' :'0');
console.log(result);

答案 1 :(得分:0)
最简单方法:
Error: Cannot assign to a reference or variable!
at _AstToIrVisitor.visitPropertyWrite (webpack-internal:///../../../compiler/esm5/compiler.js:26550:23)
at PropertyWrite.visit (webpack-internal:///../../../compiler/esm5/compiler.js:4895:24)
at convertActionBinding (webpack-internal:///../../../compiler/esm5/compiler.js:26000:45)
at eval (webpack-internal:///../../../compiler/esm5/compiler.js:28519:22)
at Array.forEach (<anonymous>)
at ViewBuilder._createElementHandleEventFn (webpack-internal:///../../../compiler/esm5/compiler.js:28515:18)
at nodes.(anonymous function) (webpack-internal:///../../../compiler/esm5/compiler.js:27934:27)
at eval (webpack-internal:///../../../compiler/esm5/compiler.js:28460:22)
at Array.map (<anonymous>)
at ViewBuilder._createNodeExpressions (webpack-internal:///../../../compiler/esm5/compiler.js:28459:56)
at _AstToIrVisitor.visitPropertyWrite (webpack-internal:///../../../compiler/esm5/compiler.js:26550:23)
at PropertyWrite.visit (webpack-internal:///../../../compiler/esm5/compiler.js:4895:24)
at convertActionBinding (webpack-internal:///../../../compiler/esm5/compiler.js:26000:45)
at eval (webpack-internal:///../../../compiler/esm5/compiler.js:28519:22)
at Array.forEach (<anonymous>)
at ViewBuilder._createElementHandleEventFn (webpack-internal:///../../../compiler/esm5/compiler.js:28515:18)
at nodes.(anonymous function) (webpack-internal:///../../../compiler/esm5/compiler.js:27934:27)
at eval (webpack-internal:///../../../compiler/esm5/compiler.js:28460:22)
at Array.map (<anonymous>)
at ViewBuilder._createNodeExpressions (webpack-internal:///../../../compiler/esm5/compiler.js:28459:56)
at resolvePromise (webpack-internal:///../../../../zone.js/dist/zone.js:824:31)
at resolvePromise (webpack-internal:///../../../../zone.js/dist/zone.js:795:17)
at eval (webpack-internal:///../../../../zone.js/dist/zone.js:873:17)
at ZoneDelegate.invokeTask (webpack-internal:///../../../../zone.js/dist/zone.js:425:31)
at Object.onInvokeTask (webpack-internal:///../../../core/esm5/core.js:4944:33)
at ZoneDelegate.invokeTask (webpack-internal:///../../../../zone.js/dist/zone.js:424:36)
at Zone.runTask (webpack-internal:///../../../../zone.js/dist/zone.js:192:47)
at drainMicroTaskQueue (webpack-internal:///../../../../zone.js/dist/zone.js:602:35)
at <anonymous>
答案 2 :(得分:0)
var array = [ 'test123', '', 'testabc' ];
var newarray = [];
array.forEach(function(item){
if(item.length > 0) {
newarray.push('1');
} else {
newarray.push('0');
}
});
console.log(newarray);
&#13;
您可以使用for或forEach
循环遍历数组如果长度大于0,我们推送值&#39; 1&#39;进入空数组 叫做 newarray ,我们推动&#39; 0&#39;。
答案 3 :(得分:0)
var array = [ 'test123', '', 'testabc' ];
var newarray = [];
array.forEach(function(item) {
if(item.trim().length > 0) {
newarray.push('1');
}
else {
newarray.push('0');
}
});
console.log(newarray);
答案 4 :(得分:-1)
您可以尝试这样的事情:
!!
1
,false将转换为0
。
var arr = [ 'test123', '', 'testabc' ];
var result = arr.map((x) => +!!x);
console.log(result)