如何使用lodash从以下输入获取这种类型的输出

时间:2019-03-25 04:47:51

标签: javascript lodash

我有以下动态生成的输入数组。

let array = ["5c8f653a247a4d513db2468b", "5c8faea05f60dd0c9447b678", "5c8faea05f60dd0c9447b678_Tue", "5c8faea05f60dd0c9447b678_Thu", "5c8faea05f60dd0c9447b678_Sat", "5c8f653a247a4d513db2468b_Sat", "5c8f653a247a4d513db2468b_Wed", "5c8f653a247a4d513db2468b_Thu", "5c8f653a247a4d513db2468b_Mon"]

此数组包含ID,例如“ 5c8f653a247a4d513db2468b”及其相关的过滤器,例如“ 5c8f653a247a4d513db2468b_Sat”

我如何获得这样的输出

let outPut = [{"id":"5c8f653a247a4d513db2468b","filter":["Mon","Thu","Wed","Sat"]},{"id":"5c8faea05f60dd0c9447b678","filter":["Tue","Thu","Sat"]}}

使用lodash或javascript

6 个答案:

答案 0 :(得分:1)

您可以使用此代码段开始编码

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

答案 1 :(得分:1)

您可以使用split拆分字符串,并将过滤日期存储在对象中。然后使用该对象创建所需的格式。

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

答案 2 :(得分:1)

如果您使用lodash,并且希望采用功能性方法,则需要:

  • 将每个项目拆分为[id,过滤器]对
  • 过滤掉只有ID的项目
  • 按ID对项目分组
  • 设置对象格式

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>

以及terser lodash / fp版本:

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>

答案 3 :(得分:0)

尝试一下:

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

答案 4 :(得分:0)

短得多的版本,没有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);

答案 5 :(得分:0)

您可以利用集合函数的lodash implicit chain sequences和集合函数的简写property()的每个变体的用法来将此数组转换为正确分组的过滤器。

  • 使用invokeMap()用定界符分割数组中的每个字符串。在这种情况下,我们将其除以_,得到['hash', 'day']的数组。
  • 使用filter()仅获得后缀为day的项目。我们将使用此收集函数的property速记变体,并评估索引索引1下day后缀的存在。
  • 请使用groupBy()(请注意速记形式),以hash前缀(位于索引0下)对每个项目进行分组。
  • 最后,使用map()通过将组的键(hash)用作id将分组的过滤器对象转换为分组的过滤器数组。通过使用速记函数变体filter来获取索引1以下的每个分组值的后缀day,从而得到结果map()数组。

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-wrapper{min-height:100%;top:0!important;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>