javascript - 嵌套数组以在搜索中查询过滤器的参数

标签 javascript ember.js filter ember-data

我有:

[
    {
        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/

相关文章:

javascript - DynamoDB Vogels 中索引 stringSet 的 ValidationException

javascript - Ember 嵌套路由和渲染模型

javascript - Ember : send action from one component to another

python - 使用 Jinja2 grouby 过滤器出现属性错误

mongodb - 过滤和排序 以下哪些查询将使用索引?

javascript - 如何在准备好 angularjs 的 $rootScope 上注册一个函数

javascript - 关于 JSF2 和 JavaScript 的警告 - JSF1064 : Unable to find or serve resource

javascript - 根据多个值或参数过滤 JSON 对象数组

javascript - 如何在 agendaDay View 中正确呈现完整日历

javascript - ember-simple-auth 的 Ember cli 错误