admin管理员组文章数量:1317906
Is there a way to display the null element when concatenated to a string?
var arr = [];
arr.push('x');
arr.push(null);
arr.push('z');
// arr = ['x', null, 'z']
var samp = 'Array elements are: ' + arr;
// Array elements are: 'x',,'z'
Output that I want:
Array elements are: 'x', null,'z'
Is there a way to display the null element when concatenated to a string?
var arr = [];
arr.push('x');
arr.push(null);
arr.push('z');
// arr = ['x', null, 'z']
var samp = 'Array elements are: ' + arr;
// Array elements are: 'x',,'z'
Output that I want:
Array elements are: 'x', null,'z'
Share
Improve this question
asked Aug 7, 2018 at 5:04
DayIsGreenDayIsGreen
2652 gold badges5 silver badges20 bronze badges
2
-
You can concatenate strings only. So check for
null
if it is null, create null as string "null" and then concatenate. null is a typeof object, so you can do JSON.stringify(null) which will be converted to string – Sivakumar Tadisetti Commented Aug 7, 2018 at 5:06 - @JavascriptLover-SKT - I'm trying to avoid inserting a "null" (as string) into the string as I also need this for SQL Queries where the queries can insert an actual string and an actual null value. – DayIsGreen Commented Aug 7, 2018 at 5:07
4 Answers
Reset to default 10You can apply String
to the arguments using Array.map. It's very succinct:
arr.map(String)
var arr = [];
arr.push('x');
arr.push(null);
arr.push('z');
var samp = 'Array elements are: ' + arr.map(String);
console.log(samp);
Implicitly coercing an array to a string calls the .join
method, and:
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
If an element is undefined or null, it is converted to the empty string.
So, you can't use implicit coercion or .join
. One option would be to use reduce
to manually join every element (so as not to skip null
s):
var arr = [];
arr.push('x');
arr.push(null);
arr.push('z');
const joined = arr.reduce((a, elm) => a + (a ? ',' : '') + elm);
var samp = 'Array elements are: ' + joined;
console.log(samp);
If you also want the quotes around each string as with your
Array elements are: 'x', null,'z'
then check the typeof
each element before concatenating:
var arr = [];
arr.push('x');
arr.push(null);
arr.push('z');
const joined = arr.reduce((a, elm) => {
const quote = typeof elm === 'string' ? "'" : '';
return a + quote + (a ? ',' : '') + elm + quote;
}, '');
var samp = 'Array elements are: ' + joined;
console.log(samp);
(if the elements contain any single-quotes themselves, you may want to replace them with \'
as well)
JSON.stringify
knows how to represent stuff in string form. If you just stringify your array, you get almost what you want:
JSON.stringify(arr)
# => ["x",null,"z"]
But if you do it yourself, you can avoid the square brackets, put a bit more space in...
arr.map(JSON.stringify).join(', ')
# => "x", null, "z"
That's an unusual yet interesting requirement. The easy thing is that you want to display null as a part of the final string. So, here's what I tried:
function arrToStr(inputArr){ //this func will return str with null
var str=undefined;
inputArr.map(function(i){str = (str!==undefined) ? (str+", "+i) : i})
return str;
}
var arr = [];
arr.push('x');
arr.push(null);
arr.push('z');
var samp = 'Array elements are: ' + arrToStr(arr);
//samp - "Array elements are: x, null, z"
Hope you got the catch here?
本文标签: Javascript insert array with null value into stringStack Overflow
版权声明:本文标题:Javascript insert array with null value into string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742030961a2416434.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论