过滤嵌套数组

时间:2018-01-13 16:11:58

标签: javascript

我正在尝试使用id过滤我的事件对象。如果其中一个事件具有类别ID匹配,那么我想将其添加到新对象。

我正在更新我的原始问题,因为dots属性是一个对象数组。所以下面的答案将不起作用

我有一个基本的fiddle

    const selectedId = 62;
    const filteredEvents = {};
    const events = {
  "2018-01-31": {
    dots: [{
      key: "test",
      color: "red",
      categories: [{
        name: "cat 1",
        id: 59
      }, {
        name: "cat 2",
        id: 61
      }, {
        name: "cat 3",
        id: 62
      }]
    }]
  },
  "2018-02-02": {
    dots: [{
      key: "test 1",
      color: "blue",
      categories: [{
        name: "cat 1",
        id: 59
      }]
    }]
  },
  "2018-02-04": {
    dots: [{
      key: "test 2",
      color: "pink",
      categories: [{
        name: "cat 1",
        id: 59
      }]
    }, {
      key: "test 2",
      color: "pink",
      categories: [{
        name: "cat 1",
        id: 59
      }]
    }]
  }
};

尝试在此处过滤:

Object.keys(events).dots.categories.filter(category => {
    category.id === selectedId // remove if matching
});

我只想在过滤后的对象中使用此属性,因为它的类别ID为62:

const events = {
  "2018-01-31": {
    dots: [{
      key: "test",
      color: "red",
      categories: [{
        name: "cat 1",
        id: 59
      }, {
        name: "cat 2",
        id: 61
      }, {
        name: "cat 3",
        id: 62
      }]
    }]
  }

4 个答案:

答案 0 :(得分:1)

您可以使用reduce构建新对象。

// Get the key for each event
let result = Object.keys(events).reduce((result, key) => {
    // Check if the categories contain a matching ID
    if (events[key].dots.categories.some(cat => cat.id === selectedId)) {
        // Add it to the results
        result[key] = events[key];
    }
    return result;
}, {});

这是一个工作片段:

const selectedId = 62;
const filteredEvents = {};
const events = {
  "2018-01-31": {
    dots: {
      key: "test",
      color: "red",
      categories: [{
        name: "cat 1",
        id: 59
      }, {
        name: "cat 2",
        id: 61
      }, {
        name: "cat 3",
        id: 62
      }]
    }
  },
  "2018-02-02": {
    dots: {
      key: "test 1",
      color: "blue",
      categories: [{
        name: "cat 1",
        id: 59
      }]
    }
  },
  "2018-02-04": {
    dots: {
      key: "test 2",
      color: "pink",
      categories: [{
        name: "cat 1",
        id: 59
      }]
    }
  }
};


let result = Object.keys(events).reduce((result, key) => {
    if (events[key].dots.categories.some(cat => cat.id === selectedId)) {
      result[key] = events[key];
    }
    return result;
}, {});


console.log(result);

答案 1 :(得分:1)

您可以使用Object.values()将对象转换为值数组.find(),以使id等于selectedId的对象

const results = Object.values(events).filter(({dots:{categories}}) => 
    categories.find(({id}) => id === selectedId)
).pop();

答案 2 :(得分:0)

您可以通过检查内部数组是否包含所需的id来过滤对象的键。

然后用给定的键构建一个新对象。



var selectedId = 62,
    filteredEvents = {},
    events = { "2018-01-31": { dots: { key: "test", color: "red", categories: [{ name: "cat 1", id: 59 }, { name: "cat 2", id: 61 }, { name: "cat 3", id: 62 }] } }, "2018-02-02": { dots: { key: "test 1", color: "blue", categories: [{ name: "cat 1", id: 59 }] } }, "2018-02-04": { dots: { key: "test 2", color: "pink", categories: [{ name: "cat 1", id: 59 }] } } };

Object.assign(filteredEvents, ...Object
    .keys(events)
    .filter(k => events[k].dots.categories.some(({ id }) => id === selectedId))
    .map(k => ({ [k]: events[k] }))
);

console.log(filteredEvents);

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




答案 3 :(得分:0)

const selectedId = 62;
const filteredEvents = {};
const events = {
  "2018-01-31": {
    dots: {
      key: "test",
      color: "red",
      categories: [{
        name: "cat 1",
        id: 59
      }, {
        name: "cat 2",
        id: 61
      }, {
        name: "cat 3",
        id: 62
      }]
    }
  },
  "2018-02-02": {
    dots: {
      key: "test 1",
      color: "blue",
      categories: [{
        name: "cat 1",
        id: 59
      }]
    }
  },
  "2018-02-04": {
    dots: {
      key: "test 2",
      color: "pink",
      categories: [{
        name: "cat 1",
        id: 59
      }]
    }
  }
};
Object.keys(events).forEach(function(element) {
   // console.log(events[element].dots.categories);
    events[element].dots.categories.forEach(function(category) {
      if (category.id === selectedId) {
        filteredEvents[element] = events[element];
      }
  });
});
console.log(filteredEvents);