简体   繁体   中英

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. 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:

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?

The object has just one key ie date whose value is a string containing all the dates. You need to split the string by , to achieve what you want and also trim() the strings to remove extra space left after splitting.

 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.

 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 ,

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())

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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