TS - 循环通过对象阵列&如果找不到,则添加到新数组

时间:2018-02-05 13:49:15

标签: javascript arrays database typescript object

我正在提供以下对象的数组:

export interface Part {
  workOrder?: string;
  task?: string;
  partNumber?: string;
  qty?: number;
  image?: string;
  name?: string;
}

我希望能够遍历每一个&从以下interface创建一个新数组:

export interface WorkOrder {
  workOrder?: string;
  tasks?: Array<string>;
}

这是我的代码的样子

let partList: Part[] = [ 
  { workOrder: "W1", task: "do something", ... },
  { workOrder: "W1", task: "something else", ... },
  { workOrder: "W2", task: "do something", ... },
  { workOrder: "W2", task: "something else", ... }
];

let workOrders: WorkOrder[] = [];

我希望能够添加workOrder&amp;从每个Part到workOrders数组的任务,但是如果已经添加了W1,那么我需要它将第二个任务“别的”添加到W1对象的tasks数组中。

最终结果应如下所示:

workOrders = [ 
  { workOrder: "W1", tasks: [ "do something", "something else" ] },
  { workOrder: "W2", tasks: [ "do something", "something else" ] }
];

我几乎让它使用一些for循环并检查某些数组的.indexOf(),但我觉得可能有更好的方法。

3 个答案:

答案 0 :(得分:1)

您可以使用reduce功能。

请查看此代码段

let partList = [
  { workOrder: "W1", task: "do something" },
  { workOrder: "W1", task: "something else" },
  { workOrder: "W2", task: "do something" },
  { workOrder: "W2", task: "something else" },
  { workOrder: "W3", task: "something else Three (3)" }
];

let workOrders = partList.reduce((a, c) => {
  var current = a.find((e) => e.workOrder === c.workOrder);
  if (current)
    current.tasks.push(c.task);
  else
    a.push({ workOrder: c.workOrder, tasks: [c.task]});
  
  return a;
}, []);

console.log(workOrders);
.as-console-wrapper {
  max-height: 100% !important
}

资源

答案 1 :(得分:1)

partList.forEach(elm => {
  if (!workOrders.find(wo => wo.workOrder == elm.workOrder)) {
      workOrders.push({ workOrder: elm.workOrder, tasks: new Array(elm.task) 
  });
  } else {
      workOrders.find(wo => wo.workOrder == 
      elm.workOrder).tasks.push(elm.task);
  }
})

您可以使用find方法查找是否添加了元素,是否添加了元素,然后推送到任务或创建新的工作单。

答案 2 :(得分:0)

您可以使用reduce方法创建包含workOrder数据的对象:

var result = partList
    .reduce<{ [name: string]: WorkOrder }>((map, r) => {
        let extsing = map[r.workOrder];
        if (extsing) {
            extsing.tasks.push(r.task);
        } else {
            map[r.workOrder] = {
                tasks: [r.task],
                workOrder: r.workOrder
            };
        }
        return map;
    }, {});

var asArray = Object.values(result)

如果您经常使用分组,则可以创建group函数来帮助完成此任务:

function group<T>(data: T[]) {
    return {
        byKey(keyGetter: (e: T) => string) {
            return {
                createGroup<TResult>(ctor: (e: T) => TResult) {
                    return {
                        addToGroup(adder: (e: T, c: TResult) => void) {
                            var result = data
                                .reduce<{ [name: string]: TResult }>((map, r) => {
                                    let key = keyGetter(r)
                                    let extsing = map[key];
                                    if (extsing) {
                                        adder(r, extsing);
                                    } else {
                                        map[key] = ctor(r);
                                    }
                                    return map;
                                }, {});

                            return Object.values(result)
                        }
                    }
                }
            }
        }
    }
}
let result = group(partList)
    .byKey(x => x.workOrder)
    .createGroup(x => <WorkOrder>{ tasks: [x.task], workOrder: x.workOrder })
    .addToGroup((e, c) => c.tasks.push(e.task));