var formRenderData = [{
  "type": "checkbox-group",
  "label": "Checkbox Group",
  "className": "checkbox-group",
  "name": "checkbox-group-1479370460494",
  "values": [{
    "label": "Option 1",
    "value": "option-1",
    "selected": true
  }, {
    "label": "Option 2",
    "value": "option-2"
  }, {
    "label": "Option 3",
    "value": "option-3"
  }]
}, {
  "type": "paragraph",
  "subtype": "p",
  "label": "Paragraph",
  "className": "paragraph"
}];

I am using grep to match object.

我正在使用grep匹配对象。

var InputName = 'checkbox-group-1479370460494';
var InputType = 'checkbox-group';

var returnedIndex = $.grep(formRenderData, function(element, index){
    if(( (element.name == InputName) || (element.label == InputName)|| (element.name == InputName.substr(0,InputName.length - 2)) ) && element.type == InputType){
        return index;
    }
});

console.log(returnedIndex);

1 个解决方案

#1


2

jQuery.grep filters an array, returning a list of the items that satisfied the filter function while leaving the original array untouched. (See the jQuery docs.)

jQuery.grep过滤一个数组,返回满足过滤功能的项目列表,同时保持原始数组不变。 (参见jQuery文档。)

I'm not sure why you wanted to access the index; I assume you wanted access to the original object that satisfied the filter. This could be accomplished by returning true from your filter function, and then looping through the matches array afterwards.

我不确定你为什么要访问索引;我假设您想要访问满足过滤器的原始对象。这可以通过从过滤器函数返回true,然后循环遍历matches数组来实现。

var InputName = 'checkbox-group-1479370460494';
var InputType = 'checkbox-group';
var formRenderData = [{"type": "checkbox-group","label": "Checkbox Group","className": "checkbox-group","name": "checkbox-group-1479370460494","values": [{"label": "Option 1","value": "option-1","selected": true},{"label": "Option 2","value": "option-2"},{"label": "Option 3","value": "option-3"}]},{"type": "paragraph","subtype": "p","label": "Paragraph","className": "paragraph"}]

var matches = $.grep(formRenderData, function(element, index){
    if(( (element.name == InputName) || (element.label == InputName)|| (element.name == InputName.substr(0,InputName.length - 2)) ) && element.type == InputType){
        return true
    }
});

console.log(matches); // [Array]
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

更多相关文章

  1. JavaScript数组操作函数方法详解
  2. 数组多重排序
  3. ES6学习笔记二之数组的扩展
  4. 在关联数组中移动元素[重复]
  5. JavaScript——数组(三)数组方法汇总
  6. 如何通过ajax将javascript数组传递给YII动作
  7. 下拉列表中的Prestashop过滤器不起作用
  8. 在Javascript中将带有空格的字符串数组转换为小写,然后在Webkit中
  9. 返回指定时间段相同间隔数组

随机推荐

  1. 更新mysql中的自动增量列+1?
  2. 8.2.1.2-MySQL如何优化 WHERE 语句
  3. 与Virtualhost Homestead相比,Localhost M
  4. ORACLE 分页SQL语句
  5. 如何在单个列上执行和操作?
  6. mysql在一个表上进行多个分组
  7. 如何在Ruby on Rails中创建tinyint(2)或tin
  8. 一些值得收藏的MySQL知识链接
  9. mysql编译安装后各种常见错误集锦
  10. 基础概念---mysql 列转行,合并字段