将数组过滤到具有更新值的另一个数组

时间:2017-09-29 15:23:26

标签: javascript angular

大家好我有一个问题我有这样的数组

  array 1 =[
    {title: 'yes', time: "00:01:50"},
    {title: 'yes', time: "00:02:50"},
    {title: 'yes', time: "00:01:50"},
    {title: 'no', time: "02:01:00"},
    {title: 'yes', time: "02:01:50"},
    {title: 'no', time: "10:01:50"},
];

我想将angular2或js中的数组按照标题过滤到另一个数组array2,就像这样

array2 = [
   {
    title :'yes',
    count:4,
    values:[
        {title: 'yes', time: "00:01:50"},
        {title: 'yes', time: "00:02:50"},
        {title: 'yes', time: "00:01:50"},
        {title: 'yes', time: "02:01:50"},
        ]
  },
  {
    title :'no',
    count:2,
    values:[
         {title: 'no', time: "02:01:00"},
         {title: 'no', time: "10:01:50"},
        ]
  }
];

其中name is the name of repeated title in the array1count is the number of repeated object by title以及values按标题存储所有重复的对象

所以过滤后我想要这样的结果

array1=[
    {title: 'yes', time: "00:01:50"},
    {title: 'no', time: "00:02:50"},
   ];

array2 = [
    {
       title :'yes',
       count:4,
       values:[
          {title: 'yes', time: "00:02:50"},
          {title: 'yes', time: "00:01:50"},
          {title: 'yes', time: "02:01:50"},
        ]
     },
     {
       title :'no',
       count:2,
       values:[
          {title: 'no', time: "10:01:50"}
        ];
    }
 ];

更多解释我正在构建一个角度为2的网站,如toggl timer,你可以在图片My website中看到,所以当我点击播放按钮并暂停另一个对象时输入值title和时间timer value已添加到数组1,这是我的angular2代码

startPlay(name:string = "", time:string = "00:00:00") {
    this.taskInputvalue.next('');
    this.play.next(true);
    this.pause.next(false);
    this.timer.next("00:00:00");
    clearInterval(this.countup);
    //push the task to array
    if(name == "") {
    name = "Add description";
    this.tasks.push({title: name, time: time});
    } else {
    name = name;
    this.tasks.push({title: name, time: time});
    }
}

但是当我重复任务名称时,就像在图片中一样,结果显示在彼此之下,所以当我第一次键入task 1时它显示为li,但是当它再次重复时它应该存储在另一个数组中,以便可以循环显示它并在我点击它时显示现有li下方的值,请访问toggl timer  理解我的意思

2 个答案:

答案 0 :(得分:1)

你需要更好地解释你需要什么,然后我可以进一步帮助你,如果这不是你想要的,因为我不明白你想要什么,我会写出我理解你需要的,如果这不是案例,评论,我们将解决它,但试着在以后的帖子中更清楚。

var input = [
    {title: 'yes', time: "00:01:50"},
    {title: 'yes', time: "00:02:50"},
    {title: 'yes', time: "00:01:50"},
    {title: 'no', time: "02:01:00"},
    {title: 'yes', time: "02:01:50"},
    {title: 'no', time: "10:01:50"},
];

function getData(arr) {
  var results = {};
  var resultArr = [];
  arr.forEach(function(el) {
        if (results[el.title]){
        results[el.title].count++;
      results[el.title].values.push(el);
    }else {
        results[el.title] = {
        title: el.title,
        count: 1,
        values: [el]
      };
    }
  })
  for (o in results){
    resultArr.push(results[o]);
  }
  return resultArr;
}

var output = getData(input);

答案 1 :(得分:0)

如果你的array1和array2是objetcs,你可以尝试这个解决方案: 假设你的array1现在重命名为array,而array2现在是newArray1和newArray2

newArray1: YourArray1ObjectTypo = [];
newArray2: YourArray2ObjectTypo = [];

// function to add the array
insertInArray2() {
  for (a1 in array) {
    if (a1.title == 'yes') {
      newArray1.push(a1);
    }
    else {
      newArray2.push(a1);
    }
  }
}

Edit-01:在您的班级中使用全局数组,这样您就不需要返回任何内容了。