如何在javascript中收集价值?

时间:2017-03-02 08:40:48

标签: javascript arrays node.js object

我在Javascript中有这样的数组。像这样的东西

[
  {
    "id": 1,
    "facilities": [
      {
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      }
    ]
  },
  {
    "id": 2,
    "facilities": [
      {
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      },
      {
        "id": 13,
        "name": "Snack",
        "label": "Snack"
      }
    ]
  },
  {
    "id": 3,
    "facilities": [
      {
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      },
      {
        "id": 14,
        "name": "Petrol",
        "label": "Petrol"
      }
    ]
  }
]

我想在Javascript中收集数据并对数组的数据设施进行分组,就像这样。

"facilities": [
      {
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      },
      {
        "id": 13,
        "name": "Snack",
        "label": "Snack"
      },
      {
        "id": 14,
        "name": "Petrol",
        "label": "Petrol"
      }
    ]

所以,基本上,按设施分组。我只是不知道如何处理类似设施值的分组。

6 个答案:

答案 0 :(得分:1)

假设设施id是唯一的:

const facilities = input.reduce((memo, entry) => {
  entry.facilities.forEach((f) => {
    if (!memo.some((m) => m.id === f.id)) {
      memo.push(f)
    }
  })
  return memo
}, [])

答案 1 :(得分:0)

您可以遍历所有行并收集(id, entity)地图 索引图允许我们每次都不搜索已收集的实体。
然后,您可以将其转换为具有对象键映射的数组。



let input = [
  {"id": 1, "facilities": [{"id": 10, "name": "Wifi", "label": "Wifi"}, {"id": 12, "name": "Toll", "label": "Toll"} ] },
  {"id": 2, "facilities": [{"id": 10, "name": "Wifi", "label": "Wifi"}, {"id": 12, "name": "Toll", "label": "Toll"}, {"id": 13, "name": "Snack", "label": "Snack"} ] },
  {"id": 3, "facilities": [{"id": 10, "name": "Wifi", "label": "Wifi"}, {"id": 12, "name": "Toll", "label": "Toll"}, {"id": 14, "name": "Petrol", "label": "Petrol"} ] }
];

let index = input.reduce((res, row) => {
  row.facilities.forEach(f => res[f.id] = f);
  return res;
}, {});

let result = Object.keys(index).map(id => index[id]);
console.log({facilities: result});




答案 2 :(得分:0)

使用Array.prototype.reduce()Set对象的解决方案:

var data = [{"id": 1,"facilities": [{"id": 10,"name": "Wifi","label": "Wifi"},{"id": 12,"name": "Toll","label": "Toll"}]},{"id": 2,"facilities": [{"id": 10,"name": "Wifi","label": "Wifi"},{"id": 12,"name": "Toll","label": "Toll"},{"id": 13,"name": "Snack","label": "Snack"}]},{"id": 3,"facilities": [{"id": 10,"name": "Wifi","label": "Wifi"},{"id": 12,"name": "Toll","label": "Toll"},{"id": 14,"name": "Petrol","label": "Petrol"}]}
];

var ids = new Set(),
    result = data.reduce(function (r, o) {
        o.facilities.forEach(function(v) {  // iterating through nested `facilities`
            if (!ids.has(v.id)) r.facilities.push(v);
            ids.add(v.id);   // saving only items with unique `id`
        });
        return r;
    }, {facilities: []});

console.log(result);

答案 3 :(得分:0)

您可以使用Set标记具有给定id的插入对象。



var data = [{ id: 1, facilities: [{ id: 10, name: "Wifi", label: "Wifi" }, { id: 12, name: "Toll", label: "Toll" }] }, { id: 2, facilities: [{ id: 10, name: "Wifi", label: "Wifi" }, { id: 12, name: "Toll", label: "Toll" }, { id: 13, name: "Snack", label: "Snack" }] }, { id: 3, facilities: [{ id: 10, name: "Wifi", label: "Wifi" }, { id: 12, name: "Toll", label: "Toll" }, { id: 14, name: "Petrol", label: "Petrol" }] }],
    grouped = data.reduce(
        (s => (r, a) => (a.facilities.forEach(b => !s.has(b.id) && s.add(b.id) && r.push(b)), r))(new Set),
        []
    );

console.log(grouped);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 4 :(得分:0)

一种非常简单易懂的方法。



const data = [{
    "id": 1,
    "facilities": [{
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      }
    ]
  },
  {
    "id": 2,
    "facilities": [{
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      },
      {
        "id": 13,
        "name": "Snack",
        "label": "Snack"
      }
    ]
  },
  {
    "id": 2,
    "facilities": [{
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      },
      {
        "id": 14,
        "name": "Petrol",
        "label": "Petrol"
      }
    ]
  }
];

let o = {};
let result = [];
data.forEach((d) => {
  d.facilities.forEach((f) => {
    o[f.id] = f;
  });
});

for (let r in o) {
  result.push(o[r]);
}

console.log(result);




答案 5 :(得分:0)

const input = [
  {
    "id": 1,
    "facilities": [
      {
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      }
    ]
  },
  {
    "id": 2,
    "facilities": [
      {
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      },
      {
        "id": 13,
        "name": "Snack",
        "label": "Snack"
      }
    ]
  },
  {
    "id": 3,
    "facilities": [
      {
        "id": 10,
        "name": "Wifi",
        "label": "Wifi"
      },
      {
        "id": 12,
        "name": "Toll",
        "label": "Toll"
      },
      {
        "id": 14,
        "name": "Petrol",
        "label": "Petrol"
      }
    ]
  }
]

const result = []
const idx = []

for (const item of input) {
  for (const facilityItem of item.facilities) {
    if (!idx.includes(facilityItem.id)) {
      idx.push(facilityItem.id)
      result.push(facilityItem) 
    }
  }
}

console.log(result)