请求格式

var abc = 123
var arr3 = [311, 312, 313, 315]
var arr2 = [
  {
    list: [{ fieldCond: 101010, fieldValue: "机器码" }],
    prodApplyId: 312
  },
  {
    list: [{ fieldCond: 10101010101, fieldValue: "设备唯一标识" }],
    prodApplyId: 312
  },
  {
    list: [{ fieldCond: 10101001, fieldValue: "设备唯一标识" }],
    prodApplyId: 312
  },
  {
    list: [{ fieldCond: 10101001, fieldValue: "设备唯一标识" }],
    prodApplyId: 312
  },
  {
    list: [{ fieldCond: 101010101, fieldValue: "设备唯一标识" }],
    prodApplyId: 311
  },
  {
    list: [{ fieldCond: 10101010101, fieldValue: "设备唯一标识" }],
    prodApplyId: 313
  },
  {
    list: [{ fieldCond: 10101010101, fieldValue: "设备唯一标识" }],
    prodApplyId: 315
  },
]


// console.log(abc)
// console.log(arr)
// console.log(arr2.prodApplyId)
// console.log(arr2)
// var r = arr2.filter(function (x) {
//   // console.log(x)
//   // if (x.prodApplyId)
//   //   return x % 2 !== 0;
//   for (var i = 0; i < arr3.length; i++) {
//     if (x.prodApplyId == arr3[i]) {
//       console.log(x.prodApplyId) //  312 312 311
//       return x.prodApplyId
//     }
//   }
// };
// const jsObjects = [
//   { a: 1, b: 2 },
//   { a: 3, b: 4 },
//   { a: 5, b: 6 },
//   { a: 7, b: 8 }
// ]
// let result;
// let aa = []
// for (var i = 0; i < arr3.length; i++) {
//   result = arr2.filter(obj => {
//     console.log(obj)
//     if (obj.prodApplyId === arr3[i]) {
//       aa.push(obj)
//       return obj.prodApplyId === arr3[i]
//     }


//   })
// }
// console.log(result)
// console.log(aa)
var arr = [
  { list: 1, prodApplyId: 1 },
  { list: 2, prodApplyId: 1 },
  { list: 3, prodApplyId: 3 },
  { list: 1, prodApplyId: 1 },
  { list: 5, prodApplyId: 5 }
]
var tb = [];
var narr = [];
for (var i = 0; i < arr.length; i++) {
  var n = tb.indexOf(arr[i].prodApplyId);
  if (n == -1) {
    tb.push(arr[i].prodApplyId);
    narr.push({ a: [arr[i].a], prodApplyId: arr[i].prodApplyId });
  } else {
    narr[n].a.push(arr[i].a);
  }
}
console.log(JSON.stringify(narr, null, 4));



var arr4 = [
  { list: [{ fieldCond: 10101001, fieldValue: "设备唯一标识" }], prodApplyId: 1 },
  { list: [{ fieldCond: 10101001, fieldValue: "设备唯一标识" }], prodApplyId: 1 },
  { list: [{ fieldCond: 10101001, fieldValue: "设备唯一标识" }], prodApplyId: 3 },
  { list: [{ fieldCond: 10101001, fieldValue: "设备唯一标识" }], prodApplyId: 1 },
  { list: [{ fieldCond: 10101001, fieldValue: "设备唯一标识" }], prodApplyId: 5 }
];
arr2 = arr2.map(function (item, index, arr) {
  console.log(item.list[0])
  const i = arr2.find(_item => item.prodApplyId === _item.prodApplyId);
  // console.log(i)
  if (i !== item) {
    // console.log(item)
    i.list.push(item.list[0]);
    return undefined;
  } else {
    // console.log(i.list[0])
    i.list = i.list;
    return i;
  }
}).filter(item => item !== undefined);
console.log(arr2);
console.log(JSON.stringify(arr2));

 

posted @ 2020-12-30 01:14  缔造cool  阅读(125)  评论(0编辑  收藏  举报