Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于嵌套对象数组中的动态键筛选数组_Javascript_Arrays_Object_Filter_Ecmascript 6 - Fatal编程技术网

Javascript 基于嵌套对象数组中的动态键筛选数组

Javascript 基于嵌套对象数组中的动态键筛选数组,javascript,arrays,object,filter,ecmascript-6,Javascript,Arrays,Object,Filter,Ecmascript 6,我有一个嵌套对象的数组。大概是这样的: const results = [ { general: { orderID: '5567', created: 1548765626101, status: 'new' }, company: { companyName: 'company x', companyEmail:

我有一个嵌套对象的数组。大概是这样的:

const results = [
    { 
        general: {
            orderID: '5567',
            created: 1548765626101,
            status: 'new'
        },

        company: {
            companyName: 'company x',
            companyEmail: 'info@companyx.com',
            companyContact: 'John Doe'
        },

        customer: {
            customerName: 'Jane Doe',
            customerEmail: 'janedoe@email.com'
        },

        products: [
            {
                productID: 4765756,
                productName: 'Product x',
                productDescription: 'Description for product x'
            },
            {
                productID: 4767839,
                productName: 'Product y',
                productDescription: 'Description for product y'
            }
        ],

        payment: {
            price: 1000,
            method: 'cash'
        }

    },
]
(为了使它有一点结构化,我只为这个问题插入了一个结果对象。但是假设结果数组中有100个元素。)

用户可以键入搜索词并选中/取消选中将包含或排除这些键的键。钥匙硬编码在列表中

比如说。用户键入“jane”并检查customerName和CustomerMail作为要搜索的关键字。或者用户键入“x”并检查productName

如何动态搜索这些选中的密钥?我已经在数组中设置了选定的关键点

因此,对于第一个示例,我得到了
['customerName','customerEmail']

第二个是
['productName']

我以前对硬编码键使用过
array.filter()
,但我不知道如何筛选这些动态键


有人能帮我把不同的步骤分解一下吗?我正在使用es6,没有外部库。

您需要迭代
结果
数组,然后深入搜索每个对象以查找匹配项。为此,你需要

  • 获取所有键/值对
  • 如果值为object,则进行更深的搜索
  • 如果值为数组,则深入搜索每个项目
  • 否则(值为字符串或数字)
    • 如果关键字在要搜索的字段列表中
    • 如果值与查询匹配,则返回true
    • 否则返回false
类似于

const deepSearcher=(字段,查询)=>
函数匹配器(对象){
常量键=对象。键(对象);
返回键。一些(键=>{
常量值=对象[键];
//句柄子数组
if(Array.isArray(value))返回value.some(matcher);
//处理子对象
if(对象的值instanceof)返回matcher(值);
//处理可测试值
if(字段包括(键)){
//处理字符串
if(typeof value==“string”)返回value.includes(查询);
//处理数字
返回值.toString()==query.toString();
}
返回false;
});
};
此函数创建一个匹配器,与
.filter
方法一起使用

const customerFilter = deepSearcher(['customerName', 'customerEmail'], 'jane')
const found = results.filter(customerFilter);
或者您可以将其直接传递到
.filter

const found = results.filter(deepSearcher(['customerName', 'customerEmail'], 'jane'));
传递给deepSearcher的字段不必属于同一对象。matcher将测试任何匹配项(但他们必须指向字符串/数字才能使代码正常工作)


工作测试用例

const结果=[{
概述:{
订单号:“5567”,
创建日期:1548765626101,
状态:“新”
},
公司:{
公司名称:“x公司”,
公司邮箱:“info@companyx.com",
公司联系人:“约翰·多伊”
},
客户:{
客户名称:“简·多伊”,
客户邮件:“janedoe@email.com"
},
产品:[{
产品编号:4765756,
产品名称:“产品x”,
productDescription:“产品x的说明”
},
{
产品编号:4767839,
产品名称:“产品y”,
productDescription:“产品y的说明”
}
],
付款:{
价格:1000,
方法:“现金”
}
}];
const deepSearcher=(字段,查询)=>
函数匹配器(对象){
常量键=对象。键(对象);
返回键。一些(键=>{
常量值=对象[键];
//句柄子数组
if(Array.isArray(value))返回value.some(matcher);
//处理子对象
if(对象的值instanceof)返回matcher(值);
//处理可测试值
if(字段包括(键)){
//处理字符串
if(typeof value==“string”)返回value.includes(查询);
//处理数字
返回值.toString()==query.toString();
}
返回false;
});
};
const matchingCustomer=results.filter(deepSearcher([“customerName”、“CustomerMail”]、“jane”);
log('results with matching customer:',matchingCustomer.length);
const matchingProduct=results.filter(deepSearcher([“productName”],“x”);
log('results with matching product:',matchingProduct.length);
const matchingPrice=results.filter(deepSearcher([“price”],“1000”);
log('匹配价格的结果:',匹配价格.length);
const nonMatchingPrice=results.filter(deepSearcher([“price”],“500”);

log('price不匹配的结果:',nonMatchingPrice.length)可能是这样的?请记住,“searchTerm”是类型敏感的

用法:搜索(结果,['companyName','productName'],'x')

/**
*返回一个对象数组,其中至少包含一个“searchKey”,其值为
*匹配“searchTerm”。
*/
功能搜索(inp、搜索键、搜索词){
设retArray=[];
功能rdp(inp、搜索键、搜索术语){
if(数组isArray(inp)){
如果(输入长度>0){
输入forEach(元素=>{
rdp(元素、搜索键、搜索术语);
});
}
}
否则{
Object.keys(inp.forEach)(prop=>{
if(Array.isArray(inp[prop])| |(inp[prop]=='object')){
rdp(inp[prop],搜索键,搜索术语);
}
否则{
searchKeys.forEach(key=>{
如果((prop==键)&&&//键匹配
(inp中的prop)){//找到搜索词
开关(inp[prop]类型){
大小写'string':if(inp[prop].indexOf(searchTerm)>-1){retArray.push(inp);}break;
案例“编号”:if(inp[prop]==searchTerm){retArray.push(inp);}break;
}
}
});
}
});
}
}
rdp(inp、搜索键、搜索术语);
返回重新排列;
}

您希望从搜索中返回什么?匹配的整个顶级对象?@GabrielePetrioli Ye