我有:
[
{
id: // the field to be search
{
eq: '1234' // eq is the operand; 1234 is the keyword
}
},
{
description:
{
like: 'desc'
}
}
]
如何将它(在 Ember.JS 中)转换为查询参数:
?filter[id][eq]=1234&filter[description][like]=desc
因为 API 需要这样的过滤器格式?
最佳答案
也许这就是您正在寻找的:
var filter = [
{
id:
{
eq: '1234'
}
},
{
description:
{
like: 'desc'
}
}
];
var output = '?';
filter.forEach(function (item) {
output += 'filter';
Object.keys(item).forEach(function (prop) {
output += '[' + prop + ']';
Object.keys(item[prop]).forEach(function (subProp) {
output += '[' + subProp + ']=' + item[prop][subProp];
});
});
output += '&';
});
output = output.substring(0, output.length - 1);
console.log(output);
您可以通过数组进行循环并使用 Object.key()使用 forEach
函数获取属性名称和值。
关于javascript - 嵌套数组以在搜索中查询过滤器的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59495294/