7月 27 遍历数组对象 Algorithm | 阅读次数 遍历数组对象12345678910111213/** 参数说明 * arr: array = 被遍历 数组proto * objKey: string = filter * key: string = 当前筛选项 */getIndex = function (arr, objKey, key) { for (let i = 0; i < arr.length; i++) { if (arr[i][objKey] === key) { return i; } } return -1;} 123456789101112131415161718192021for (const item of callback.data.items) { let inArr = getIndex(this.imgsCascade, "value", item.imgMode) let thisObj = array[inArr] if (inArr > -1) { thisObj.children ? thisObj.children.push(children) : thisObj.children = [children]; } else { array.push( { value: item.imgMode, label: item.imgItem, children: [{xxx:xxx, yyy:yyy}] } ) }} 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051{function getIndex(arr, key) { for (let i = 0; i < arr.length; i++) { if (arr[i].key === key) { return i; } } return -1;}function getKey(obj) { return obj.key;}var arr = [ { key: '1' }, { key: '2' }, { key: '3' }]var a = { key: '1'}var c = { key: '1'}var b = { key: '4'}function pushToArr(val) { if (getIndex(arr, getKey(val)) > -1) { if (arr[getIndex(arr, getKey(val))].children) { arr[getIndex(arr, getKey(val))].children.push(val) } else { arr[getIndex(arr, getKey(val))].children = [val] } } else { arr.push(val) }}pushToArr(a)console.log(arr);pushToArr(c)console.log(arr);} 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134{ function hasKey(arr, key) { var ret = false; for (let i = 0; i < arr.length; i++) { if (arr[i].key === key) { ret = true; break; } } return ret; } function getKey(obj) { return obj.key; } var arr = [{ key: '1', children: [ ] }, { key: '2', children: [ ] }, { key: '3', children: [ ] } ] var a = { key: '1' } var b = { key: '4' } console.log(hasKey(arr, getKey(a))); console.log(hasKey(arr, getKey(b)));}{ function getIndex(arr, objKey, key) { for (let i = 0; i < arr.length; i++) { if (arr[i].key === key) { return i; } } return -1; } function getKey(obj) { return obj.key; } var arr = [{ key: '1' }, { key: '2' }, { key: '3' }] var a = { key: '1' } var b = { key: '4' } function pushToArr(val) { if (getIndex(arr, getKey(val)) > -1) { arr[getIndex(arr, getKey(val))].children = [val] } else { arr.push(val) } } pushToArr(b) console.log(arr);}{ function getIndex(arr, key) { for (let i = 0; i < arr.length; i++) { if (arr[i].key === key) { return i; } } return -1; } function getKey(obj) { return obj.key; } var arr = [{ key: '1' }, { key: '2' }, { key: '3' }] var a = { key: '1' } var c = { key: '1' } var b = { key: '4' } function pushToArr(val) { if (getIndex(arr, getKey(val)) > -1) { if (arr[getIndex(arr, getKey(val))].children) { arr[getIndex(arr, getKey(val))].children.push(val) } else { arr[getIndex(arr, getKey(val))].children = [val] } } else { arr.push(val) } } pushToArr(a) console.log(arr); pushToArr(c) console.log(arr);} 上一篇 git update 下一篇 node.js Set-Cookie header属性