javascript 筛选对象属性以获取数据的最佳方法

llmtgqce  于 2023-04-28  发布在  Java
关注(0)|答案(2)|浏览(190)

我试图通过搜索表单过滤一些API数据,但不确定如何获得所需的结果,也不确定我哪里出错了。
我调用一个API,它返回一个对象数组,看起来像这样:

const apiData = [
    {
        "firstName": "chris",
        "lastName": "kundill",
        "postcode": "sa11 3fj"
    },
    {
        "firstName": "sam",
        "lastName": "white",
        "postcode": null
    },
    {
        "firstName": "john",
        "lastName": "davies",
        "postcode": "sa11 3fj"
    },
    {
        "firstName": "jane",
        "lastName": "davies",
        "postcode": "sa11 3fj"
    },
    {
        "firstName": "maria",
        "lastName": "davies",
        "postcode": "sa11 3fj"
    },
    {
        "firstName": "nutter",
        "lastName": "dave",
        "postcode": "sa11 3fj"
    },
    {
        "firstName": "donna",
        "lastName": "duggie",
        "postcode": "sa11 3fj"
    }
]

我已经将搜索表单数据存储在一个对象中,它看起来像这样,NB。不是所有3个输入字段都必须填写:-

const formData = {
    "firstName": "Chris",
    "postcode": "SA11 3FJ"
}

然后,我获取formData对象,并将值转换为小写,并通过以下操作删除任何空字符串:

const sanitise = Object.values(formData).map((item) => {
      return item.toLowerCase()
    }).filter((item) => {
      if (item !== '') {
        return item
      }
    })

给我:-

const sanitise = ['chris', 'sa11 3fj']

如何过滤API数据以返回包含sanitise数组数据的正确对象?
我一直在使用array.filter方法,但我无法找到正确的条件组合,以仅返回正确的对象,而不返回包含"postcode": "sa11 3fj"的每个对象

igetnqfo

igetnqfo1#

如果在清理表单数据后将其作为对象保存,则可以针对数据集中的每个对象检查其键/值对,而无需在搜索函数中硬编码键。

const data=[{firstName:"chris",lastName:"kundill",postcode:"sa11 3fj"},{firstName:"sam",lastName:"white",postcode:null},{firstName:"john",lastName:"davies",postcode:"sa11 3fj"},{firstName:"jane",lastName:"davies",postcode:"sa11 3fj"},{firstName:"maria",lastName:"davies",postcode:"sa11 3fj"},{firstName:"nutter",lastName:"dave",postcode:"sa11 3fj"},{firstName:"donna",lastName:"duggie",postcode:"sa11 3fj"}];

// `reduce` over the formData entries.
// If the current prop value exists coerce it
// to lowercase (and add the property to the accumulator)
function sanitiseFormData(formData) {
  const formEntries = Object.entries(formData);
  return formEntries.reduce((acc, [ key, value ]) => {
    if (value) acc[key] = value.toLowerCase();
    return acc;
  }, {});
}

// `search` accepts the data, and the sanitised query
// `filter` over the data and for each object iterate over
// the queryEntries and if every one of the key/value pairs in
// the query matches the key/value pairs in the current object
// return the object
function search(data, query) {
  const queryEntries = Object.entries(query);
  return data.filter(obj => {
    return queryEntries.every(([ key, value ]) => {
      return obj[key] === value;
    });
  });
}

const formData1 = {
  firstName: 'Chris',
  lastName: '',
  postcode: 'SA11 3FJ'
};

const formData2 = {
  firstName: 'Donna',
  lastName: null,
  postcode: 'SA11 3FJ'
};

console.log(search(data, sanitiseFormData(formData1)));
console.log(search(data, sanitiseFormData(formData2)));

其他文件

jvlzgdj9

jvlzgdj92#

好吧,如果你只想解决这个问题,这对我来说很有效:

let filtered = apiData.filter((obj) => obj["firstName"] == sanitise[0] && obj["postcode"] == sanitise[1]); // Just combine the 2 conditions
console.log(filtered);

我不确定我应该用let还是var还是const来表示filtered变量,哈哈

相关问题