Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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_Jquery - Fatal编程技术网

Javascript 如何基于字符串匹配提取嵌套对象?

Javascript 如何基于字符串匹配提取嵌套对象?,javascript,jquery,Javascript,Jquery,activePath将根据api调用动态更改,如何根据嵌套对象中匹配的activePath字符串提取对象 路径示例:Drug/GetRefills在这种情况下,它应该推送data.Drug.GetRefills,如果路径是Payment/getAccount它应该推送data.Payment.getAccount main.js const data = [{ id: 11, name: "Drug", children: [{

activePath将根据api调用动态更改,如何根据嵌套对象中匹配的activePath字符串提取对象

路径示例:
Drug/GetRefills
在这种情况下,它应该推送
data.Drug.GetRefills
,如果路径是
Payment/getAccount
它应该推送
data.Payment.getAccount

main.js

const data = 
    [{
        id: 11,
        name: "Drug",
        children: [{
            id: 12,
            name: "getRefills"
        }, {
            id: 13,
            name: "getDetails"
        }]
    }, {
        id: 14,
        name: "Payment",
        children: [{
            id: 15,
            name: "getAccount"
        }, {
            id: 16,
            name: "getAccountDetails"
        }]
    }]


  function getModelData(data){
        var activePath = "Drug/GetRefills";
        var _interfaces = [];   
        $.each(data, function(id, item){
            if (activePath.toLowerCase().includes(item.name)) {
                console.log('OBJ', item);
                _interfaces.push(item);   // it should push getrefills object into interfaces   
            } 
        });

         return _interfaces;
    }

可以使用递归查找对象(类似于DFS):

const数据=[{
id:11,
名称:“毒品”,
儿童:[{
id:12,
名称:“getRefills”
}, {
id:13,
名称:“getDetails”
}]
}, {
身份证号码:14,
名称:“付款”,
儿童:[{
身份证号码:15,
名称:“getAccount”
}, {
身份证号码:16,
名称:“getAccountDetails”
}]
}];
函数getModelData(路径){
函数查找(arr、[key、…rest]){
const obj=arr.find(o=>o.name==key);
如果(!obj)返回空值;
返回rest.length?查找(obj.children | |[],rest):obj;
}
返回find(数据,path.split('/');
//将找到的对象添加到_接口,而不是返回
}
日志(getModelData('Drug/getRefills');
日志(getModelData('Drug/getRefillsss');

log(getModelData('Payment/getAccountDetails')您可以使用递归查找对象(类似于DFS):

const数据=[{
id:11,
名称:“毒品”,
儿童:[{
id:12,
名称:“getRefills”
}, {
id:13,
名称:“getDetails”
}]
}, {
身份证号码:14,
名称:“付款”,
儿童:[{
身份证号码:15,
名称:“getAccount”
}, {
身份证号码:16,
名称:“getAccountDetails”
}]
}];
函数getModelData(路径){
函数查找(arr、[key、…rest]){
const obj=arr.find(o=>o.name==key);
如果(!obj)返回空值;
返回rest.length?查找(obj.children | |[],rest):obj;
}
返回find(数据,path.split('/');
//将找到的对象添加到_接口,而不是返回
}
日志(getModelData('Drug/getRefills');
日志(getModelData('Drug/getRefillsss');

log(getModelData('Payment/getAccountDetails')我想您正在寻找一些类似于util的平板电脑。这是一个非常基本的例子

const data = [
  {
    id: 11,
    name: "Drug",
    children: [
      {
        id: 12,
        name: "getRefills"
      },
      {
        id: 13,
        name: "getDetails"
      }
    ]
  },
  {
    id: 14,
    name: "Payment",
    children: [
      {
        id: 15,
        name: "getAccount"
      },
      {
        id: 16,
        name: "getAccountDetails"
      }
    ]
  }
];
function flat(array) {
    return array.reduce((m, {name, children}) => {
        children.forEach((child) => {
            const {name:cname} = child
            const fullName = `${name.toLowerCase()}/${cname.toLowerCase()}`
            if(!m[fullName]) m[fullName] =[]
            m[fullName].push(child)
        })
        return m
    },{})
}

function getModelData(path, data) {
  return flat(data)[path.toLowerCase()];
}
var activePath = "Drug/GetRefills";
console.log(getModelData(activePath, data));

//Output
[ { id: 12, name: 'getRefills' } ]

我想你是在找公寓之类的。这是一个非常基本的例子

const data = [
  {
    id: 11,
    name: "Drug",
    children: [
      {
        id: 12,
        name: "getRefills"
      },
      {
        id: 13,
        name: "getDetails"
      }
    ]
  },
  {
    id: 14,
    name: "Payment",
    children: [
      {
        id: 15,
        name: "getAccount"
      },
      {
        id: 16,
        name: "getAccountDetails"
      }
    ]
  }
];
function flat(array) {
    return array.reduce((m, {name, children}) => {
        children.forEach((child) => {
            const {name:cname} = child
            const fullName = `${name.toLowerCase()}/${cname.toLowerCase()}`
            if(!m[fullName]) m[fullName] =[]
            m[fullName].push(child)
        })
        return m
    },{})
}

function getModelData(path, data) {
  return flat(data)[path.toLowerCase()];
}
var activePath = "Drug/GetRefills";
console.log(getModelData(activePath, data));

//Output
[ { id: 12, name: 'getRefills' } ]

下面是一个示例,说明如何使用filter函数从数据中获取正确的对象

const activePath=“药物/getRefills”;
//得名
让name=activePath.substr(0,activePath.indexOf('/');
//获取详细信息
让detail=activePath.substr(activePath.indexOf('/')+1);
常数数据=
[{
id:11,
名称:“毒品”,
儿童:[{
id:12,
名称:“getRefills”
}, {
id:13,
名称:“getDetails”
}]
}, {
身份证号码:14,
名称:“付款”,
儿童:[{
身份证号码:15,
名称:“getAccount”
}, {
身份证号码:16,
名称:“getAccountDetails”
}]
}]
//使用lodash过滤器获取数据
const currentName=u2;.filter(数据,d=>d.name==name);
const currentDetail=u389;.filter(currentName[0]。子项,c=>c.name==detail);
console.log(currentDetail[0])

下面是一个示例,说明如何使用筛选函数从数据中获取正确的对象

const activePath=“药物/getRefills”;
//得名
让name=activePath.substr(0,activePath.indexOf('/');
//获取详细信息
让detail=activePath.substr(activePath.indexOf('/')+1);
常数数据=
[{
id:11,
名称:“毒品”,
儿童:[{
id:12,
名称:“getRefills”
}, {
id:13,
名称:“getDetails”
}]
}, {
身份证号码:14,
名称:“付款”,
儿童:[{
身份证号码:15,
名称:“getAccount”
}, {
身份证号码:16,
名称:“getAccountDetails”
}]
}]
//使用lodash过滤器获取数据
const currentName=u2;.filter(数据,d=>d.name==name);
const currentDetail=u389;.filter(currentName[0]。子项,c=>c.name==detail);
console.log(currentDetail[0])

@Taplar修复了语法问题
.toLowerCase()
看起来像是您的问题。您或者应该删除它,因为“drug/getrefills”不包括“drug”。或者将路径和项都小写。name@Taplar修复了语法问题
.toLowerCase()
看起来像是您的问题。您或者应该删除它,因为“drug/getrefills”不包括“drug”。或者将路径和项都小写。名称我是否需要转换为小写?我在查找时得到nullmethod@aftab啊,是的,如果您传入的密钥有奇怪的大小写,您可以在
arr.find
检查中将两者转换为小写。类似于
const obj=arr.find(o=>o.name.toLowerCase()==key.toLowerCase())
我刚刚注意到activePath有时没有像just
Refills那样的
get
函数,所以我可能会使用不同的路径来包含完整的路径,比如
app.Api.Drug.getRefills
基于这个逻辑,上面的代码更改是什么,我尝试了抛出null?@aftab这是一个更难的问题。也许您可以使用
includes()
()进行测试,而不是检查键和名称之间是否完全相等。我需要转换为小写吗?我在查找时得到nullmethod@aftab啊,是的,如果您传入的密钥有奇怪的大小写,您可以在
arr.find
检查中将两者转换为小写。类似于
const obj=arr.find(o=>o.name.toLowerCase()==key.toLowerCase())
我刚刚注意到activePath有时没有像just
Refills那样的
get
函数,所以我可能会使用不同的路径来包含完整的路径,比如
app.Api.Drug.getRefills
基于这个逻辑,上面的代码更改是什么,我尝试了抛出null?@aftab这是一个更难的问题。您可以使用
includes()
()进行测试,而不是检查key和name之间是否完全相等。