How to get this type of output from following input using lodashHow do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How can I convert a string to boolean in JavaScript?How to get the children of the $(this) selector?How do I loop through or enumerate a JavaScript object?How can I get query string values in JavaScript?How to merge two arrays in JavaScript and de-duplicate itemsHow to decide when to use Node.js?How do I remove a particular element from an array in JavaScript?How do I return the response from an asynchronous call?
In a Fish that is not a Fish
Probability Dilemma
How is linear momentum conserved in circular motion?
Can I define two primary keys in a database table & why?
Is there a risk to write an invitation letter for a stranger to obtain a Czech (Schengen) visa?
How does a particle move under a constant 4-force?
Right indicator flash-frequency has increased and rear-right bulb is out
You may find me... puzzling
Time at 1G acceleration to travel 100 000 light years
What is the context for Napoleon's quote "[the Austrians] did not know the value of five minutes"?
Is it possible to use just one shared folder for log shipping?
I'm yearning in grey
How could I create a situation in which a PC has to make a saving throw or be forced to pet a dog?
How do credit card companies know what type of business I'm paying for?
Is it a bad idea to have a pen name with only an initial for a surname?
How to recover a single blank shot from a film camera
Will users know a CardView is clickable?
How can I ping multiple IP addresses at the same time?
Is a sequel allowed to start before the end of the first book?
How to write a nice frame challenge?
What is this plant I saw for sale at a Romanian farmer's market?
Excel round 0.34 to 0.35
Should I email my professor to clear up a (possibly very irrelevant) awkward misunderstanding?
Is using Legacy mode is a bad thing to do?
How to get this type of output from following input using lodash
How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How can I convert a string to boolean in JavaScript?How to get the children of the $(this) selector?How do I loop through or enumerate a JavaScript object?How can I get query string values in JavaScript?How to merge two arrays in JavaScript and de-duplicate itemsHow to decide when to use Node.js?How do I remove a particular element from an array in JavaScript?How do I return the response from an asynchronous call?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have the following input Array which is dynamically generated.
let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"]
This Array contain the Ids e.g "5c8f653a247a4d513db2468b" and it related filter e.g "5c8f653a247a4d513db2468b_Sat"
How I get the output like this
let outPut = ["id":"5c8f653a247a4d513db2468b","filter":["Mon","Thu","Wed","Sat"],"id":"5c8faea05f60dd0c9447b678","filter":["Tue","Thu","Sat"]}
using lodash or javascript
javascript lodash
add a comment |
I have the following input Array which is dynamically generated.
let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"]
This Array contain the Ids e.g "5c8f653a247a4d513db2468b" and it related filter e.g "5c8f653a247a4d513db2468b_Sat"
How I get the output like this
let outPut = ["id":"5c8f653a247a4d513db2468b","filter":["Mon","Thu","Wed","Sat"],"id":"5c8faea05f60dd0c9447b678","filter":["Tue","Thu","Sat"]}
using lodash or javascript
javascript lodash
Your output isn't valid. Is it supposed to be an array of two objects?
– JasonB
Mar 25 at 5:05
Sorry, I have updated the out mistake. I need weekday in an array with related Ids.
– Malik Adil
Mar 25 at 5:10
add a comment |
I have the following input Array which is dynamically generated.
let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"]
This Array contain the Ids e.g "5c8f653a247a4d513db2468b" and it related filter e.g "5c8f653a247a4d513db2468b_Sat"
How I get the output like this
let outPut = ["id":"5c8f653a247a4d513db2468b","filter":["Mon","Thu","Wed","Sat"],"id":"5c8faea05f60dd0c9447b678","filter":["Tue","Thu","Sat"]}
using lodash or javascript
javascript lodash
I have the following input Array which is dynamically generated.
let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"]
This Array contain the Ids e.g "5c8f653a247a4d513db2468b" and it related filter e.g "5c8f653a247a4d513db2468b_Sat"
How I get the output like this
let outPut = ["id":"5c8f653a247a4d513db2468b","filter":["Mon","Thu","Wed","Sat"],"id":"5c8faea05f60dd0c9447b678","filter":["Tue","Thu","Sat"]}
using lodash or javascript
javascript lodash
javascript lodash
edited Mar 25 at 5:09
Malik Adil
asked Mar 25 at 4:47
Malik AdilMalik Adil
12210
12210
Your output isn't valid. Is it supposed to be an array of two objects?
– JasonB
Mar 25 at 5:05
Sorry, I have updated the out mistake. I need weekday in an array with related Ids.
– Malik Adil
Mar 25 at 5:10
add a comment |
Your output isn't valid. Is it supposed to be an array of two objects?
– JasonB
Mar 25 at 5:05
Sorry, I have updated the out mistake. I need weekday in an array with related Ids.
– Malik Adil
Mar 25 at 5:10
Your output isn't valid. Is it supposed to be an array of two objects?
– JasonB
Mar 25 at 5:05
Your output isn't valid. Is it supposed to be an array of two objects?
– JasonB
Mar 25 at 5:05
Sorry, I have updated the out mistake. I need weekday in an array with related Ids.
– Malik Adil
Mar 25 at 5:10
Sorry, I have updated the out mistake. I need weekday in an array with related Ids.
– Malik Adil
Mar 25 at 5:10
add a comment |
6 Answers
6
active
oldest
votes
You can start coding using this code segment
var _ = require('lodash');
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const regualarData = _.filter(array, item => item.includes("_"));
const plainData = _.map(regualarData, item =>
return
id: item.substring(0, item.indexOf("_")),
day: item.substring(item.indexOf("_")+1),
;
);
const nested = _.groupBy(plainData, 'id');
const response = [];
for (var key in nested)
if (nested.hasOwnProperty(key))
response.push(id: key , filter: _.map(nested[key],t => t.day));
console.log(response);
add a comment |
You can split the string using split, and store the filter days in an object. Then use that object to create the desired format.
let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
//console.log( array );
// remove items with no underscore
array = array.filter( x => x.indexOf( '_' ) != -1 );
//console.log( array );
// create sortedData object
const sortedData = ;
array.forEach( function( x )
const parts = x.split( '_' );
const key = parts[0];
const value = parts[1];
if ( ! ( key in sortedData ) )
sortedData[key] = [];
sortedData[key].push( value );
);
//console.log( sortedData );
const output = [];
for ( let key in sortedData )
if ( sortedData.hasOwnProperty( key ) )
output.push( id: key, filters: sortedData[key] );
console.log( output );
add a comment |
If you use lodash, and want to take the functional approach, you need to:
- Split each item to a [id, filter] pair
- Filter out items that only have the id
- Group items by the id
- Format the objects
const flow, partialRight: pr, split, filter, groupBy, first, last, map = _;
const fn = flow(
pr(map, item => split(item, '_')), // convert to [id, filter] pairs
pr(filter, length: 2 ), // filter out items without filter
pr(groupBy, first), // group by the 1st item in each pair
pr(map, (group, id) => ( // format the objects
id,
filter: map(group, last) // get the filters from the pairs
))
)
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = fn(array);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
And the terser lodash/fp version:
const flow, split, filter, groupBy, first, last, map = _;
const fn = flow(
map(split('_')), // convert to [id, filter] pairs
filter( length: 2 ), // filters items without filter
groupBy(first), // group by the 1st item in each pair
map(group => ( // format the objects
id: first(first(group)), // take the id from the first item
filter: map(last, group) // get the filters from the pairs
))
)
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = fn(array);
console.log(result);
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
1
Kudos for the comments and not registering any new states!
– Paul Stenne
Mar 25 at 8:07
add a comment |
Try this:
let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"]
var tmpArr = [];
var tmpDay = [];
var output = [];
for(var i = 0; i < array.length; i++)
var tmp = array[i].split("_");
if(tmp.length == 1)
tmpArr.push(array[i]);
tmpDay.push([]);
else
tmpDay[tmpArr.indexOf(tmp[0])].push(tmp[1]);
for(var i = 0; i < tmpArr.length; i++)
var tmpObj = ;
tmpObj.id = tmpArr[i];
tmpObj.filter = tmpDay[i];
output.push(tmpObj);
console.log(output);
add a comment |
Much shorter version without loadash.
var array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
array = array.map(v => v.split('_'));
var arr = ;
array.map(ar =>
if(arr.hasOwnProperty(ar[0]))
arr[ar[0]].push(ar[1]);
else
arr[ar[0]] = [];
);
var _arr = [];
for(key in arr)
_arr.push(id:key, filter:arr[key]);
console.log(_arr);
add a comment |
You can make use of the lodash implicit chain sequences of collection functions and the usage of each of the collection functions' shorthand property() variations to transform this array into a properly grouped filter.
- Use invokeMap() to split each string in the array by a delimiter. In this case, we split it by
_
, resulting in an array of['hash', 'day']
. - Use filter() to only get the items with a
day
suffix. We'll use theproperty
shorthand variation of this collection function and evaluate the existence ofday
suffix which is under index 1. - Use groupBy(), note the shorthand variation, to group each item with the
hash
prefix which is under index 0. - Lastly, use map() to transform the grouped filter object into a grouped filter array by using the key (
hash
) of the group as theid
. The resultingfilter
array is obtained by getting theday
suffix of each of the grouped value which is under index 1 by usingmap()
, shorthand function variation.
const result = _(array)
.invokeMap('split', '_')
.filter(1)
.groupBy(0)
.map((v, id) => ( id, filter: _.map(v, 1) ))
.value();
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = _(array)
.invokeMap('split', '_')
.filter(1)
.groupBy(0)
.map((v, id) => ( id, filter: _.map(v, 1) ))
.value();
console.log(result);
.as-console-wrappermin-height:100%;top:0!important;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331400%2fhow-to-get-this-type-of-output-from-following-input-using-lodash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can start coding using this code segment
var _ = require('lodash');
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const regualarData = _.filter(array, item => item.includes("_"));
const plainData = _.map(regualarData, item =>
return
id: item.substring(0, item.indexOf("_")),
day: item.substring(item.indexOf("_")+1),
;
);
const nested = _.groupBy(plainData, 'id');
const response = [];
for (var key in nested)
if (nested.hasOwnProperty(key))
response.push(id: key , filter: _.map(nested[key],t => t.day));
console.log(response);
add a comment |
You can start coding using this code segment
var _ = require('lodash');
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const regualarData = _.filter(array, item => item.includes("_"));
const plainData = _.map(regualarData, item =>
return
id: item.substring(0, item.indexOf("_")),
day: item.substring(item.indexOf("_")+1),
;
);
const nested = _.groupBy(plainData, 'id');
const response = [];
for (var key in nested)
if (nested.hasOwnProperty(key))
response.push(id: key , filter: _.map(nested[key],t => t.day));
console.log(response);
add a comment |
You can start coding using this code segment
var _ = require('lodash');
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const regualarData = _.filter(array, item => item.includes("_"));
const plainData = _.map(regualarData, item =>
return
id: item.substring(0, item.indexOf("_")),
day: item.substring(item.indexOf("_")+1),
;
);
const nested = _.groupBy(plainData, 'id');
const response = [];
for (var key in nested)
if (nested.hasOwnProperty(key))
response.push(id: key , filter: _.map(nested[key],t => t.day));
console.log(response);
You can start coding using this code segment
var _ = require('lodash');
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const regualarData = _.filter(array, item => item.includes("_"));
const plainData = _.map(regualarData, item =>
return
id: item.substring(0, item.indexOf("_")),
day: item.substring(item.indexOf("_")+1),
;
);
const nested = _.groupBy(plainData, 'id');
const response = [];
for (var key in nested)
if (nested.hasOwnProperty(key))
response.push(id: key , filter: _.map(nested[key],t => t.day));
console.log(response);
answered Mar 25 at 5:14
safwanmohasafwanmoha
161110
161110
add a comment |
add a comment |
You can split the string using split, and store the filter days in an object. Then use that object to create the desired format.
let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
//console.log( array );
// remove items with no underscore
array = array.filter( x => x.indexOf( '_' ) != -1 );
//console.log( array );
// create sortedData object
const sortedData = ;
array.forEach( function( x )
const parts = x.split( '_' );
const key = parts[0];
const value = parts[1];
if ( ! ( key in sortedData ) )
sortedData[key] = [];
sortedData[key].push( value );
);
//console.log( sortedData );
const output = [];
for ( let key in sortedData )
if ( sortedData.hasOwnProperty( key ) )
output.push( id: key, filters: sortedData[key] );
console.log( output );
add a comment |
You can split the string using split, and store the filter days in an object. Then use that object to create the desired format.
let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
//console.log( array );
// remove items with no underscore
array = array.filter( x => x.indexOf( '_' ) != -1 );
//console.log( array );
// create sortedData object
const sortedData = ;
array.forEach( function( x )
const parts = x.split( '_' );
const key = parts[0];
const value = parts[1];
if ( ! ( key in sortedData ) )
sortedData[key] = [];
sortedData[key].push( value );
);
//console.log( sortedData );
const output = [];
for ( let key in sortedData )
if ( sortedData.hasOwnProperty( key ) )
output.push( id: key, filters: sortedData[key] );
console.log( output );
add a comment |
You can split the string using split, and store the filter days in an object. Then use that object to create the desired format.
let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
//console.log( array );
// remove items with no underscore
array = array.filter( x => x.indexOf( '_' ) != -1 );
//console.log( array );
// create sortedData object
const sortedData = ;
array.forEach( function( x )
const parts = x.split( '_' );
const key = parts[0];
const value = parts[1];
if ( ! ( key in sortedData ) )
sortedData[key] = [];
sortedData[key].push( value );
);
//console.log( sortedData );
const output = [];
for ( let key in sortedData )
if ( sortedData.hasOwnProperty( key ) )
output.push( id: key, filters: sortedData[key] );
console.log( output );
You can split the string using split, and store the filter days in an object. Then use that object to create the desired format.
let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
//console.log( array );
// remove items with no underscore
array = array.filter( x => x.indexOf( '_' ) != -1 );
//console.log( array );
// create sortedData object
const sortedData = ;
array.forEach( function( x )
const parts = x.split( '_' );
const key = parts[0];
const value = parts[1];
if ( ! ( key in sortedData ) )
sortedData[key] = [];
sortedData[key].push( value );
);
//console.log( sortedData );
const output = [];
for ( let key in sortedData )
if ( sortedData.hasOwnProperty( key ) )
output.push( id: key, filters: sortedData[key] );
console.log( output );
let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
//console.log( array );
// remove items with no underscore
array = array.filter( x => x.indexOf( '_' ) != -1 );
//console.log( array );
// create sortedData object
const sortedData = ;
array.forEach( function( x )
const parts = x.split( '_' );
const key = parts[0];
const value = parts[1];
if ( ! ( key in sortedData ) )
sortedData[key] = [];
sortedData[key].push( value );
);
//console.log( sortedData );
const output = [];
for ( let key in sortedData )
if ( sortedData.hasOwnProperty( key ) )
output.push( id: key, filters: sortedData[key] );
console.log( output );
let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
//console.log( array );
// remove items with no underscore
array = array.filter( x => x.indexOf( '_' ) != -1 );
//console.log( array );
// create sortedData object
const sortedData = ;
array.forEach( function( x )
const parts = x.split( '_' );
const key = parts[0];
const value = parts[1];
if ( ! ( key in sortedData ) )
sortedData[key] = [];
sortedData[key].push( value );
);
//console.log( sortedData );
const output = [];
for ( let key in sortedData )
if ( sortedData.hasOwnProperty( key ) )
output.push( id: key, filters: sortedData[key] );
console.log( output );
answered Mar 25 at 5:15
JasonBJasonB
4,90321124
4,90321124
add a comment |
add a comment |
If you use lodash, and want to take the functional approach, you need to:
- Split each item to a [id, filter] pair
- Filter out items that only have the id
- Group items by the id
- Format the objects
const flow, partialRight: pr, split, filter, groupBy, first, last, map = _;
const fn = flow(
pr(map, item => split(item, '_')), // convert to [id, filter] pairs
pr(filter, length: 2 ), // filter out items without filter
pr(groupBy, first), // group by the 1st item in each pair
pr(map, (group, id) => ( // format the objects
id,
filter: map(group, last) // get the filters from the pairs
))
)
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = fn(array);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
And the terser lodash/fp version:
const flow, split, filter, groupBy, first, last, map = _;
const fn = flow(
map(split('_')), // convert to [id, filter] pairs
filter( length: 2 ), // filters items without filter
groupBy(first), // group by the 1st item in each pair
map(group => ( // format the objects
id: first(first(group)), // take the id from the first item
filter: map(last, group) // get the filters from the pairs
))
)
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = fn(array);
console.log(result);
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
1
Kudos for the comments and not registering any new states!
– Paul Stenne
Mar 25 at 8:07
add a comment |
If you use lodash, and want to take the functional approach, you need to:
- Split each item to a [id, filter] pair
- Filter out items that only have the id
- Group items by the id
- Format the objects
const flow, partialRight: pr, split, filter, groupBy, first, last, map = _;
const fn = flow(
pr(map, item => split(item, '_')), // convert to [id, filter] pairs
pr(filter, length: 2 ), // filter out items without filter
pr(groupBy, first), // group by the 1st item in each pair
pr(map, (group, id) => ( // format the objects
id,
filter: map(group, last) // get the filters from the pairs
))
)
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = fn(array);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
And the terser lodash/fp version:
const flow, split, filter, groupBy, first, last, map = _;
const fn = flow(
map(split('_')), // convert to [id, filter] pairs
filter( length: 2 ), // filters items without filter
groupBy(first), // group by the 1st item in each pair
map(group => ( // format the objects
id: first(first(group)), // take the id from the first item
filter: map(last, group) // get the filters from the pairs
))
)
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = fn(array);
console.log(result);
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
1
Kudos for the comments and not registering any new states!
– Paul Stenne
Mar 25 at 8:07
add a comment |
If you use lodash, and want to take the functional approach, you need to:
- Split each item to a [id, filter] pair
- Filter out items that only have the id
- Group items by the id
- Format the objects
const flow, partialRight: pr, split, filter, groupBy, first, last, map = _;
const fn = flow(
pr(map, item => split(item, '_')), // convert to [id, filter] pairs
pr(filter, length: 2 ), // filter out items without filter
pr(groupBy, first), // group by the 1st item in each pair
pr(map, (group, id) => ( // format the objects
id,
filter: map(group, last) // get the filters from the pairs
))
)
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = fn(array);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
And the terser lodash/fp version:
const flow, split, filter, groupBy, first, last, map = _;
const fn = flow(
map(split('_')), // convert to [id, filter] pairs
filter( length: 2 ), // filters items without filter
groupBy(first), // group by the 1st item in each pair
map(group => ( // format the objects
id: first(first(group)), // take the id from the first item
filter: map(last, group) // get the filters from the pairs
))
)
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = fn(array);
console.log(result);
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
If you use lodash, and want to take the functional approach, you need to:
- Split each item to a [id, filter] pair
- Filter out items that only have the id
- Group items by the id
- Format the objects
const flow, partialRight: pr, split, filter, groupBy, first, last, map = _;
const fn = flow(
pr(map, item => split(item, '_')), // convert to [id, filter] pairs
pr(filter, length: 2 ), // filter out items without filter
pr(groupBy, first), // group by the 1st item in each pair
pr(map, (group, id) => ( // format the objects
id,
filter: map(group, last) // get the filters from the pairs
))
)
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = fn(array);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
And the terser lodash/fp version:
const flow, split, filter, groupBy, first, last, map = _;
const fn = flow(
map(split('_')), // convert to [id, filter] pairs
filter( length: 2 ), // filters items without filter
groupBy(first), // group by the 1st item in each pair
map(group => ( // format the objects
id: first(first(group)), // take the id from the first item
filter: map(last, group) // get the filters from the pairs
))
)
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = fn(array);
console.log(result);
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
const flow, partialRight: pr, split, filter, groupBy, first, last, map = _;
const fn = flow(
pr(map, item => split(item, '_')), // convert to [id, filter] pairs
pr(filter, length: 2 ), // filter out items without filter
pr(groupBy, first), // group by the 1st item in each pair
pr(map, (group, id) => ( // format the objects
id,
filter: map(group, last) // get the filters from the pairs
))
)
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = fn(array);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
const flow, partialRight: pr, split, filter, groupBy, first, last, map = _;
const fn = flow(
pr(map, item => split(item, '_')), // convert to [id, filter] pairs
pr(filter, length: 2 ), // filter out items without filter
pr(groupBy, first), // group by the 1st item in each pair
pr(map, (group, id) => ( // format the objects
id,
filter: map(group, last) // get the filters from the pairs
))
)
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = fn(array);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
const flow, split, filter, groupBy, first, last, map = _;
const fn = flow(
map(split('_')), // convert to [id, filter] pairs
filter( length: 2 ), // filters items without filter
groupBy(first), // group by the 1st item in each pair
map(group => ( // format the objects
id: first(first(group)), // take the id from the first item
filter: map(last, group) // get the filters from the pairs
))
)
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = fn(array);
console.log(result);
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
const flow, split, filter, groupBy, first, last, map = _;
const fn = flow(
map(split('_')), // convert to [id, filter] pairs
filter( length: 2 ), // filters items without filter
groupBy(first), // group by the 1st item in each pair
map(group => ( // format the objects
id: first(first(group)), // take the id from the first item
filter: map(last, group) // get the filters from the pairs
))
)
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = fn(array);
console.log(result);
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
answered Mar 25 at 8:01
Ori DroriOri Drori
87.1k1595103
87.1k1595103
1
Kudos for the comments and not registering any new states!
– Paul Stenne
Mar 25 at 8:07
add a comment |
1
Kudos for the comments and not registering any new states!
– Paul Stenne
Mar 25 at 8:07
1
1
Kudos for the comments and not registering any new states!
– Paul Stenne
Mar 25 at 8:07
Kudos for the comments and not registering any new states!
– Paul Stenne
Mar 25 at 8:07
add a comment |
Try this:
let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"]
var tmpArr = [];
var tmpDay = [];
var output = [];
for(var i = 0; i < array.length; i++)
var tmp = array[i].split("_");
if(tmp.length == 1)
tmpArr.push(array[i]);
tmpDay.push([]);
else
tmpDay[tmpArr.indexOf(tmp[0])].push(tmp[1]);
for(var i = 0; i < tmpArr.length; i++)
var tmpObj = ;
tmpObj.id = tmpArr[i];
tmpObj.filter = tmpDay[i];
output.push(tmpObj);
console.log(output);
add a comment |
Try this:
let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"]
var tmpArr = [];
var tmpDay = [];
var output = [];
for(var i = 0; i < array.length; i++)
var tmp = array[i].split("_");
if(tmp.length == 1)
tmpArr.push(array[i]);
tmpDay.push([]);
else
tmpDay[tmpArr.indexOf(tmp[0])].push(tmp[1]);
for(var i = 0; i < tmpArr.length; i++)
var tmpObj = ;
tmpObj.id = tmpArr[i];
tmpObj.filter = tmpDay[i];
output.push(tmpObj);
console.log(output);
add a comment |
Try this:
let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"]
var tmpArr = [];
var tmpDay = [];
var output = [];
for(var i = 0; i < array.length; i++)
var tmp = array[i].split("_");
if(tmp.length == 1)
tmpArr.push(array[i]);
tmpDay.push([]);
else
tmpDay[tmpArr.indexOf(tmp[0])].push(tmp[1]);
for(var i = 0; i < tmpArr.length; i++)
var tmpObj = ;
tmpObj.id = tmpArr[i];
tmpObj.filter = tmpDay[i];
output.push(tmpObj);
console.log(output);
Try this:
let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"]
var tmpArr = [];
var tmpDay = [];
var output = [];
for(var i = 0; i < array.length; i++)
var tmp = array[i].split("_");
if(tmp.length == 1)
tmpArr.push(array[i]);
tmpDay.push([]);
else
tmpDay[tmpArr.indexOf(tmp[0])].push(tmp[1]);
for(var i = 0; i < tmpArr.length; i++)
var tmpObj = ;
tmpObj.id = tmpArr[i];
tmpObj.filter = tmpDay[i];
output.push(tmpObj);
console.log(output);
answered Mar 25 at 5:16
Adnan TokyAdnan Toky
732412
732412
add a comment |
add a comment |
Much shorter version without loadash.
var array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
array = array.map(v => v.split('_'));
var arr = ;
array.map(ar =>
if(arr.hasOwnProperty(ar[0]))
arr[ar[0]].push(ar[1]);
else
arr[ar[0]] = [];
);
var _arr = [];
for(key in arr)
_arr.push(id:key, filter:arr[key]);
console.log(_arr);
add a comment |
Much shorter version without loadash.
var array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
array = array.map(v => v.split('_'));
var arr = ;
array.map(ar =>
if(arr.hasOwnProperty(ar[0]))
arr[ar[0]].push(ar[1]);
else
arr[ar[0]] = [];
);
var _arr = [];
for(key in arr)
_arr.push(id:key, filter:arr[key]);
console.log(_arr);
add a comment |
Much shorter version without loadash.
var array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
array = array.map(v => v.split('_'));
var arr = ;
array.map(ar =>
if(arr.hasOwnProperty(ar[0]))
arr[ar[0]].push(ar[1]);
else
arr[ar[0]] = [];
);
var _arr = [];
for(key in arr)
_arr.push(id:key, filter:arr[key]);
console.log(_arr);
Much shorter version without loadash.
var array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
array = array.map(v => v.split('_'));
var arr = ;
array.map(ar =>
if(arr.hasOwnProperty(ar[0]))
arr[ar[0]].push(ar[1]);
else
arr[ar[0]] = [];
);
var _arr = [];
for(key in arr)
_arr.push(id:key, filter:arr[key]);
console.log(_arr);
var array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
array = array.map(v => v.split('_'));
var arr = ;
array.map(ar =>
if(arr.hasOwnProperty(ar[0]))
arr[ar[0]].push(ar[1]);
else
arr[ar[0]] = [];
);
var _arr = [];
for(key in arr)
_arr.push(id:key, filter:arr[key]);
console.log(_arr);
var array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
array = array.map(v => v.split('_'));
var arr = ;
array.map(ar =>
if(arr.hasOwnProperty(ar[0]))
arr[ar[0]].push(ar[1]);
else
arr[ar[0]] = [];
);
var _arr = [];
for(key in arr)
_arr.push(id:key, filter:arr[key]);
console.log(_arr);
answered Mar 25 at 5:39
Prabu samvelPrabu samvel
992314
992314
add a comment |
add a comment |
You can make use of the lodash implicit chain sequences of collection functions and the usage of each of the collection functions' shorthand property() variations to transform this array into a properly grouped filter.
- Use invokeMap() to split each string in the array by a delimiter. In this case, we split it by
_
, resulting in an array of['hash', 'day']
. - Use filter() to only get the items with a
day
suffix. We'll use theproperty
shorthand variation of this collection function and evaluate the existence ofday
suffix which is under index 1. - Use groupBy(), note the shorthand variation, to group each item with the
hash
prefix which is under index 0. - Lastly, use map() to transform the grouped filter object into a grouped filter array by using the key (
hash
) of the group as theid
. The resultingfilter
array is obtained by getting theday
suffix of each of the grouped value which is under index 1 by usingmap()
, shorthand function variation.
const result = _(array)
.invokeMap('split', '_')
.filter(1)
.groupBy(0)
.map((v, id) => ( id, filter: _.map(v, 1) ))
.value();
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = _(array)
.invokeMap('split', '_')
.filter(1)
.groupBy(0)
.map((v, id) => ( id, filter: _.map(v, 1) ))
.value();
console.log(result);
.as-console-wrappermin-height:100%;top:0!important;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
add a comment |
You can make use of the lodash implicit chain sequences of collection functions and the usage of each of the collection functions' shorthand property() variations to transform this array into a properly grouped filter.
- Use invokeMap() to split each string in the array by a delimiter. In this case, we split it by
_
, resulting in an array of['hash', 'day']
. - Use filter() to only get the items with a
day
suffix. We'll use theproperty
shorthand variation of this collection function and evaluate the existence ofday
suffix which is under index 1. - Use groupBy(), note the shorthand variation, to group each item with the
hash
prefix which is under index 0. - Lastly, use map() to transform the grouped filter object into a grouped filter array by using the key (
hash
) of the group as theid
. The resultingfilter
array is obtained by getting theday
suffix of each of the grouped value which is under index 1 by usingmap()
, shorthand function variation.
const result = _(array)
.invokeMap('split', '_')
.filter(1)
.groupBy(0)
.map((v, id) => ( id, filter: _.map(v, 1) ))
.value();
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = _(array)
.invokeMap('split', '_')
.filter(1)
.groupBy(0)
.map((v, id) => ( id, filter: _.map(v, 1) ))
.value();
console.log(result);
.as-console-wrappermin-height:100%;top:0!important;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
add a comment |
You can make use of the lodash implicit chain sequences of collection functions and the usage of each of the collection functions' shorthand property() variations to transform this array into a properly grouped filter.
- Use invokeMap() to split each string in the array by a delimiter. In this case, we split it by
_
, resulting in an array of['hash', 'day']
. - Use filter() to only get the items with a
day
suffix. We'll use theproperty
shorthand variation of this collection function and evaluate the existence ofday
suffix which is under index 1. - Use groupBy(), note the shorthand variation, to group each item with the
hash
prefix which is under index 0. - Lastly, use map() to transform the grouped filter object into a grouped filter array by using the key (
hash
) of the group as theid
. The resultingfilter
array is obtained by getting theday
suffix of each of the grouped value which is under index 1 by usingmap()
, shorthand function variation.
const result = _(array)
.invokeMap('split', '_')
.filter(1)
.groupBy(0)
.map((v, id) => ( id, filter: _.map(v, 1) ))
.value();
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = _(array)
.invokeMap('split', '_')
.filter(1)
.groupBy(0)
.map((v, id) => ( id, filter: _.map(v, 1) ))
.value();
console.log(result);
.as-console-wrappermin-height:100%;top:0!important;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
You can make use of the lodash implicit chain sequences of collection functions and the usage of each of the collection functions' shorthand property() variations to transform this array into a properly grouped filter.
- Use invokeMap() to split each string in the array by a delimiter. In this case, we split it by
_
, resulting in an array of['hash', 'day']
. - Use filter() to only get the items with a
day
suffix. We'll use theproperty
shorthand variation of this collection function and evaluate the existence ofday
suffix which is under index 1. - Use groupBy(), note the shorthand variation, to group each item with the
hash
prefix which is under index 0. - Lastly, use map() to transform the grouped filter object into a grouped filter array by using the key (
hash
) of the group as theid
. The resultingfilter
array is obtained by getting theday
suffix of each of the grouped value which is under index 1 by usingmap()
, shorthand function variation.
const result = _(array)
.invokeMap('split', '_')
.filter(1)
.groupBy(0)
.map((v, id) => ( id, filter: _.map(v, 1) ))
.value();
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = _(array)
.invokeMap('split', '_')
.filter(1)
.groupBy(0)
.map((v, id) => ( id, filter: _.map(v, 1) ))
.value();
console.log(result);
.as-console-wrappermin-height:100%;top:0!important;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = _(array)
.invokeMap('split', '_')
.filter(1)
.groupBy(0)
.map((v, id) => ( id, filter: _.map(v, 1) ))
.value();
console.log(result);
.as-console-wrappermin-height:100%;top:0!important;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
const array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"];
const result = _(array)
.invokeMap('split', '_')
.filter(1)
.groupBy(0)
.map((v, id) => ( id, filter: _.map(v, 1) ))
.value();
console.log(result);
.as-console-wrappermin-height:100%;top:0!important;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
answered Mar 25 at 9:41
ryeballarryeballar
24.2k94661
24.2k94661
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331400%2fhow-to-get-this-type-of-output-from-following-input-using-lodash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your output isn't valid. Is it supposed to be an array of two objects?
– JasonB
Mar 25 at 5:05
Sorry, I have updated the out mistake. I need weekday in an array with related Ids.
– Malik Adil
Mar 25 at 5:10