返回具有与搜索条件匹配的键的数组的所有索引

时间:2017-05-26 12:23:05

标签: javascript arrays javascript-objects

我有一个对象数组,我需要在数组中获取他们的位置,其中关键的ecommerce_order_number值与我搜索的数字相匹配。

我试过做一个for循环,但我可以看到这不是一个好主意,因为我可能会对我所拥有的单独数组进行500+检查。

通常我会循环并获得所有比赛并继续离开那里。但我希望有一个更清洁的方法使用vanilla javascript。

var myTransactions = [];
myTransactions[0].order = 'S17243';


for(i=0; i < myTransactions.length; i++)
{
// 
}

但是我可以看到我的循环将仅限于交易计数,如果有更多的付款与确实发生的交易数量相比,我将无法获得匹配的所有支付数组的索引

是否有一个函数会搜索一个对象数组并返回找到的匹配特定字符串的所有键的索引?

var payments=    [{
        "id": "11419",
        "recordtype": "payment",
        "cols": {
            "entity": {
                "name": "Angela smith",
                "id": "6641"
            },
            "account": {
                "name": "test-data",
                "id": "335"
            },
            "amount": 3810.2,
            "ecommerce_order_number": "S17247",
            "datecreated": "10/4/2017 5:42 PM"
        }
    }, {
        "id": "11420",
        "recordtype": "payment",
        "cols": {
            "entity": {
                "name": "Paul Georgeson",
                "id": "6640"
            },
            "account": {
                "name": "test-data",
                "id": "335"
            },
            "amount": 3539,
            "ecommerce_order_number": "S17223",
            "datecreated": "10/4/2017 5:42 PM"
        }
    }, {
        "id": "11421",
        "recordtype": "payment",
        "cols": {
            "entity": {
                "name": "Leanne Smithy",
                "id": "6638"
            },
            "account": {
                "name": "test-data",
                "id": "336"
            },
            "amount": 1617.2,
            "ecommerce_order_number": "S17243",
            "datecreated": "10/4/2017 5:42 PM"
        }
    }];

4 个答案:

答案 0 :(得分:0)

var payments=    [{
    "id": "11419",
    "recordtype": "payment",
    "cols": {
        "entity": {
            "name": "Angela smith",
            "id": "6641"
        },
        "account": {
            "name": "test-data",
            "id": "335"
        },
        "amount": 3810.2,
        "ecommerce_order_number": "S17247",
        "datecreated": "10/4/2017 5:42 PM"
    }
}, {
    "id": "11420",
    "recordtype": "payment",
    "cols": {
        "entity": {
            "name": "Paul Georgeson",
            "id": "6640"
        },
        "account": {
            "name": "test-data",
            "id": "335"
        },
        "amount": 3539,
        "ecommerce_order_number": "S17223",
        "datecreated": "10/4/2017 5:42 PM"
    }
}, {
    "id": "11421",
    "recordtype": "payment",
    "cols": {
        "entity": {
            "name": "Leanne Smithy",
            "id": "6638"
        },
        "account": {
            "name": "test-data",
            "id": "336"
        },
        "amount": 1617.2,
        "ecommerce_order_number": "S17243",
        "datecreated": "10/4/2017 5:42 PM"
    }
}];


var ids=payments.reduce((arr,el,i)=>(el.cols&&el.cols.ecommerce_order_number.includes("S17243")&&!arr.push(i))||a‌​rr,[]);

您可以使用Array.prototype.reduce方法......

长片:

var ids=payments.reduce((arr,el,i)=>{
 if(el.cols&&el.cols.ecommerce_order_number.includes("S17243")) arr.push(i);
return arr;
},[]);

http://jsbin.com/jucexilude/edit?console

答案 1 :(得分:0)

我有类似的情况,我使用Array.map做了。

var payments=    [{
        "id": "11419",
        "recordtype": "payment",
        "cols": {
            "entity": {
                "name": "Angela smith",
                "id": "6641"
            },
            "account": {
                "name": "test-data",
                "id": "335"
            },
            "amount": 3810.2,
            "ecommerce_order_number": "S17247",
            "datecreated": "10/4/2017 5:42 PM"
        }
    }, {
        "id": "11420",
        "recordtype": "payment",
        "cols": {
            "entity": {
                "name": "Paul Georgeson",
                "id": "6640"
            },
            "account": {
                "name": "test-data",
                "id": "335"
            },
            "amount": 3539,
            "ecommerce_order_number": "S17223",
            "datecreated": "10/4/2017 5:42 PM"
        }
    }, {
        "id": "11421",
        "recordtype": "payment",
        "cols": {
            "entity": {
                "name": "Leanne Smithy",
                "id": "6638"
            },
            "account": {
                "name": "test-data",
                "id": "336"
            },
            "amount": 1617.2,
            "ecommerce_order_number": "S17243",
            "datecreated": "10/4/2017 5:42 PM"
        }
    }];

var arrOfIndexes = payments.map((payment, index, completeArr) => {
                       if(payment.cols.ecommerce_order_number === "S17243") {
                          return index;
                       } else { return null }
                   }).filter(Boolean); // filter used to remove falsey values
console.log(arrOfIndexes);

答案 2 :(得分:0)

您可以使用jQuery grep函数来检索具有特定属性值的所有对象,然后您可以根据需要获取ID,请参阅以下内容:

使用ecommerce_order_number =“S17243”获取所有对象:

var filteredObj = $.grep(payments, function(obj) {
    return obj.cols.ecommerce_order_number === "S17243";
});

仅获取上述对象的ID:

var onlyIds = filteredObj.map(function(obj) {return obj.id;});

将以上代码合并为一行说明:

var oneLineObj = $.grep(payments, function(obj) {
    return obj.cols.ecommerce_order_number === "S17243";
}).map(function(obj) {return obj.id;});

请参见以下示例:

var payments=    [{
        "id": "11419",
        "recordtype": "payment",
        "cols": {
            "entity": {
                "name": "Angela smith",
                "id": "6641"
            },
            "account": {
                "name": "test-data",
                "id": "335"
            },
            "amount": 3810.2,
            "ecommerce_order_number": "S17247",
            "datecreated": "10/4/2017 5:42 PM"
        }
    }, {
        "id": "11420",
        "recordtype": "payment",
        "cols": {
            "entity": {
                "name": "Paul Georgeson",
                "id": "6640"
            },
            "account": {
                "name": "test-data",
                "id": "335"
            },
            "amount": 3539,
            "ecommerce_order_number": "S17223",
            "datecreated": "10/4/2017 5:42 PM"
        }
    }, {
        "id": "11421",
        "recordtype": "payment",
        "cols": {
            "entity": {
                "name": "Leanne Smithy",
                "id": "6638"
            },
            "account": {
                "name": "test-data",
                "id": "336"
            },
            "amount": 1617.2,
            "ecommerce_order_number": "S17243",
            "datecreated": "10/4/2017 5:42 PM"
        }
    }, {
        "id": "test",
        "recordtype": "payment",
        "cols": {
            "entity": {
                "name": "Alessandro",
                "id": "1"
            },
            "account": {
                "name": "test-data",
                "id": "336"
            },
            "amount": 5000.2,
            "ecommerce_order_number": "S17243",
            "datecreated": "18/5/2017 5:42 PM"
        }
    }];


var filteredObj = $.grep(payments, function(obj) {
    return obj.cols.ecommerce_order_number === "S17243";
});

var onlyIds = filteredObj.map(function(obj) {return obj.id;});

console.log("IDS: ", onlyIds);
console.log("COMPLETE OBJECTS: ", filteredObj);

var oneLineObj = $.grep(payments, function(obj) {
    return obj.cols.ecommerce_order_number === "S17243";
}).map(function(obj) {return obj.id;});

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

答案 3 :(得分:0)

纯javascript,没有jquery解决方案。

您可以使用新的数组助手:.filter.find

.filter解决方案,如果您希望有多个结果。

const resultFiler = payments.filter(payment => {
  if (payment.cols.ecommerce_order_number === searchValue) {
    return payment;
  }
}).map(payment => payment.id);

.filter helper只返回完整的数组元素。我们不能做return payment.id之类的事情,因为我们只关心.id属性。

要获得.id字符,我们使用了.map(payment => payment.id);

.find solution 如果您只期望一个结果。

const resultFind = payments.find(payment => {
  if (payment.cols.ecommerce_order_number === searchValue) {
    return payment;
  }
}).id;

如果你不熟悉胖箭头/ ES6语法,这里是等效的代码:

var resultFiler = payments.filter(function (payment) {
  if (payment.cols.ecommerce_order_number === searchValue) {
    return payment;
  }
}).map(function (payment) {
  return payment.id;
});

// Using find
var resultFind = payments.find(function (payment) {
  if (payment.cols.ecommerce_order_number === searchValue) {
    return payment;
  }
}).id;

参见工作示例。

var payments=    [{
        "id": "11419",
        "recordtype": "payment",
        "cols": {
            "entity": {
                "name": "Angela smith",
                "id": "6641"
            },
            "account": {
                "name": "test-data",
                "id": "335"
            },
            "amount": 3810.2,
            "ecommerce_order_number": "S17247",
            "datecreated": "10/4/2017 5:42 PM"
        }
    }, {
        "id": "11420",
        "recordtype": "payment",
        "cols": {
            "entity": {
                "name": "Paul Georgeson",
                "id": "6640"
            },
            "account": {
                "name": "test-data",
                "id": "335"
            },
            "amount": 3539,
            "ecommerce_order_number": "S17223",
            "datecreated": "10/4/2017 5:42 PM"
        }
    }, {
        "id": "11421",
        "recordtype": "payment",
        "cols": {
            "entity": {
                "name": "Leanne Smithy",
                "id": "6638"
            },
            "account": {
                "name": "test-data",
                "id": "336"
            },
            "amount": 1617.2,
            "ecommerce_order_number": "S17243",
            "datecreated": "10/4/2017 5:42 PM"
        }
    }];
    
const searchValue = "S17243"
// Find all matching occurrences
    
const resultFiler = payments.filter(payment => {
  if (payment.cols.ecommerce_order_number === searchValue) {
    return payment;
  }
}).map(payment => payment.id);


// Using find
const resultFind = payments.find(payment => {
  if (payment.cols.ecommerce_order_number === searchValue) {
    return payment;
  }
}).id;

console.log(resultFiler);
console.log(resultFind);