javascript 过滤和排序对象

shstlldc  于 2022-12-21  发布在  Java
关注(0)|答案(2)|浏览(160)

输入数据示例json文件:

{"data": [{"name": "Kyle", "email": "kyle2@mail.com"},
{"name": "Kyle", "email": "kyle1@mail.com"},
{"name": "Kate", "email": "kate@mail.com"}]}

筛选和排序条件示例json文件:

{"condition": {"include": [{"name": "Kyle"}], "sort_by": ["email"]}}

结果:

{"result": [{"name": "Kyle", "email": "kyle1@mail.com"},
{"name": "Kyle", "email": "kyle2@mail.com"}]}

逻辑应该是什么?.filter和.sort是处理此任务的最佳方式吗?

const array = [];
const filterCondition = {}

async function fetchAsync() {
  const response = await fetch("./input.json");
  const data = await response.json();

// get array to work with

  data.data.forEach((item) => {
    array.push(item);
  });

// here I tried to get condition to the variable, so later I can use .filter method

  const filterCondition = data.condition.exclude[0];

// I understand that this is bad way... And you could also have more than one condition in that array.. 
// But how can you access that condition in the json condition file?

}

fetchAsync();

另外,过滤条件可以是“include”或“exclude”。我只想做这样的if语句

if (include) {
   array.filter(e => e.xxx === condition.xxx)
} else {
   array.filter(e => e.xxx !== condition.xxx)
}

请帮助我们了解自觉的逻辑。

rqqzpn5f

rqqzpn5f1#

这将适用于多个include和多个sort_by选项。
我假设所有的值在比较时都是字符串。你可以根据需要扩展它。

const input = {
  data: [
    { name: "Kyle", code: "ASDF", email: "kyle1@mail.com", web: "kyle.com" },
    { name: "Kyle", code: "ASDF", email: "kyle1@mail.com", web: "kyle.au" },
    { name: "Kate", code: "notASDF", email: "kate@mail.com" },
  ],
};

const rule = {
  condition: {
    include: [{ name: "Kyle" }, { code: "ASDF" }],
    sort_by: ["email", "web"],
  },
};

const output = input.data
  .filter((item) =>
    rule.condition.include.every((obj) =>
      Object.entries(obj).reduce(
        (cond, [key, value]) => cond && item[key]?.includes(value),
        true
      )
    )
  )
  .sort((a, b) =>
    rule.condition.sort_by
      .map((key) => a[key].localeCompare(b[key]))
      .reduce((prev, curr) => prev || curr, 0)
  );

console.log(output);

使用Object.entries()Array.prototype.every()Array.prototype.reduce()Array.prototype.filter()

a6b3iqyw

a6b3iqyw2#

当输入和规则在同一个文件中时,它可以工作,但是当我获取它们时,我得到了一个错误
“未捕获(承诺中)类型错误:项[键]?.includes不是函数”
无法理解。输入和规则对象在提取后完全相同。

const input = {
  data: [],
};
const rule = {};

async function fetchAsync() {
  const response = await fetch("./input.json");
  const data = await response.json();

  data.data.forEach((item) => {
    input.data.push(item);
  });

  rule.condition = data.condition;

  console.log(input);
  console.log(rule);

  const output = input.data
    .filter((item) =>
      rule.condition.include.every((obj) =>
        Object.entries(obj).reduce(
          (cond, [key, value]) => cond && item[key]?.includes(value),
          true
        )
      )
    )
    .sort((a, b) =>
      rule.condition.sort_by
        .map((key) => a[key].localeCompare(b[key]))
        .reduce((prev, curr) => prev || curr, 0)
    );

  console.log(output);
}

fetchAsync();

相关问题