简体   繁体   English

我可以将.map数组转换为带双引号的逗号分隔数组吗?

[英]Can I turn .map array into a comma separated array with double quotes?

I am tryibng to take some data from a set of twig variables and parse them into a javascript plugin. 我正在尝试从一组树枝变量中获取一些数据,并将其解析为javascript插件。 They are a set of dates listed in an array. 它们是数组中列出的一组日期。 They are first rendered onto the template like so as a string: 它们首先像字符串一样呈现在模板上:

{"date":"2018-08-30, 2018-08-13, 2018-08-10, 2018-08-01, 2018-08-26, 2018-09-27, 2018-09-26, 2018-09-25, 2018-09-24, 2018-09-23, 2018-09-29, 2018-09-28"}

I then take this data and use it to create an array with the below function: 然后,我获取这些数据,并使用它通过以下函数创建一个数组:

const datesNotAvailableArray = $('#datesGone').data('dates');
var arr = Object.keys(datesNotAvailableArray).map(function(k) { 
    return datesNotAvailableArray[k] 
});

When I console.log(arr[0]) I get the below output: 当我console.log(arr[0])我得到以下输出:

2018-08-30, 2018-08-13, 2018-08-10, 2018-08-01, 2018-08-26, 2018-09-27, 2018-09-26, 2018-09-25, 2018-09-24, 2018-09-23, 2018-09-29, 2018-09-28

This is fine but what I really want to get is this sort of thing: 很好,但是我真正想要得到的是这种东西:

["2018-08-30", "2018-08-13", "2018-08-10", etc etc]

Can I do this within the .map function itself? 我可以在.map函数本身中执行此操作吗?

The object has just one key ie date whose value is a string containing all the dates. 该对象只有一个键,即date其值是一个包含所有日期的字符串。 You need to split the string by , to achieve what you want and also trim() the strings to remove extra space left after splitting. 您需要用分割字符串,以实现所需的内容,还需要trim()字符串以删除分割后剩余的空间。

 const dateObj = {"date":"2018-08-30, 2018-08-13, 2018-08-10, 2018-08-01, 2018-08-26, 2018-09-27, 2018-09-26, 2018-09-25, 2018-09-24, 2018-09-23, 2018-09-29, 2018-09-28"} var dates = dateObj.date.split(",").map(s => s.trim()); console.log(dates); 

You could use split() to turn the string into array, then, if you want the array-like string with double quotes, you could use .map and return '"' + e + '"' in each iteration, and then join(",") again to return the string. 您可以使用split()将字符串转换为数组,然后,如果要使用双引号将类似数组的字符串,则可以使用.map并在每次迭代中返回'"' + e + '"' ,然后join(",")再次返回字符串。

 const obj = {"date":"2018-08-30, 2018-08-13, 2018-08-10, 2018-08-01, 2018-08-26, 2018-09-27, 2018-09-26, 2018-09-25, 2018-09-24, 2018-09-23, 2018-09-29, 2018-09-28"} var newArr = obj.date.split(",");//<-- (", ") could remove the spaces in this case console.log(newArr)//<-- array newArr = newArr.map(e=>'"' + e + '"') str = "["+newArr.join(",")+"]" console.log(str)//<-- string with " as your example 

Simple do it with String.prototype.split() , It'll return an array of strings by separating the string into substrings , 使用String.prototype.split()轻松完成此操作,它将通过将字符串分成字符串来返回字符串数组,

See the extra space after , on split() method, it'll help you to prevent using extra methods like map() and trim() eg date.split(',').map(i=>i.trim()) 后见额外的空间 ,split()方法,它会帮助你避免使用额外的方法,如map()trim()date.split(',').map(i=>i.trim())

 var dates = { "date": "2018-08-30, 2018-08-13, 2018-08-10, 2018-08-01, 2018-08-26, 2018-09-27, 2018-09-26, 2018-09-25, 2018-09-24, 2018-09-23, 2018-09-29, 2018-09-28" }; console.log(dates.date.split(", ")); 

const datesNotAvailableArray = $('#datesGone').data('dates');
datesNotAvailableArray['date'].split(', ')

 var obj = {"date":"2018-08-30, 2018-08-13, 2018-08-10, 2018-08-01, 2018-08-26, 2018-09-27, 2018-09-26, 2018-09-25, 2018-09-24, 2018-09-23, 2018-09-29, 2018-09-28"} const result = obj .date.split(',').map(s => s.trim()); console.log(result) 

I think you're looking more for something like this: 我认为您正在寻找更多类似这样的东西:

var arr = Object.keys(datesNotAvailableArray).map(k => obj[k].split(','))[0]
console.log(arr[0); // ["2018-08-30", " 2018-08-13", " 2018-08-10", " 2018-08-01", " 2018-08-26", " 2018-09-27", " 2018-09-26", " 2018-09-25", " 2018-09-24", " 2018-09-23", " 2018-09-29", " 2018-09-28"]

It would get the result you want but it's really only useful if you have other keys from your source object. 它会得到您想要的结果,但实际上仅在源对象中有其他键时才有用。 However, I would advice against it as doing so would lose information as to which output array index was mapped from which key of the source object. 但是,我建议这样做,因为这样做会丢失有关从源对象的哪个键映射到哪个输出数组索引的信息。

Any of the previous answers would be a better approach than what you were trying to do with .map 与您尝试使用.map相比,以前的任何答案都将是更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM