我想在下面的响应中获得第一个具有In [37]: np.multiply(vec1,vec2)
Out[37]: array([ 32.4, -87.8, -0. , -52.2, -24.4])
的项目。在这种情况下,预期响应应为"shipping":"ABC"
我不太擅长用Postman编写Javascript测试。
谢谢。
37
答案 0 :(得分:0)
这将返回带有"shipping": "ABC"
的第一项:
// convert response data to array of key-value pairs
const entries = Object.entries(response);
// reduce array to the first matching object
const reducer = (acc, [key, val]) => {
return (acc === null && val.shipping === "ABC") ?
(acc = { [key]: val }) : null;
};
// return the found object or null
const found = entries.reduce(reducer, null);
/*
{
'37': {
shipping_id: 37,
position: 0,
status: 'D',
shipping: 'ABC',
delivery_time: '24h-72h'
}
}
*/
工作示例:
const res = {
"37": {
shipping_id: 37,
position: 0,
status: "D",
shipping: "ABC",
delivery_time: "24h-72h"
},
"36": {
shipping_id: 36,
position: 0,
status: "D",
shipping: "DEF",
delivery_time: ""
},
"28": {
shipping_id: 28,
position: 0,
status: "D",
shipping: "GHI",
delivery_time: ""
}
};
const entries = Object.entries(res);
const reducer = (acc, [key, val]) => {
return (acc === null && val.shipping === "ABC") ?
(acc = { [key]: val }) : null;
};
const found = entries.reduce(reducer, null);
console.log(found);
答案 1 :(得分:0)
我从另一篇文章中找到了解决方法:
var response = JSON.parse(responseBody);
const result = Object.keys(response).find(v => response[v].shipping === 'ABC');
//your object keys are equal to id, you can just return key
console.log(result);
// if your object keys can be different from id you can do this
console.log(response[result].shipping_id);