对象通过键获取值
Object get the value by a key
我遇到了一个问题。这是我的 AJAX 电话的回复。
var test = [
{
"analytics.feature": "false"
},
{
"demo": "true"
},
{
"analytics.demo": "false"
}
]
现在我想检查 analytics.demo 是启用还是禁用。
我可以使用 _.find 吗?
_.find(test, {analytics.demo});
//returns 错误
我只想做这个
if(analytics.demo) {
//Do something
}
我该怎么做?有人可以帮忙吗?
除了迭代对象和比较,我可以使用 underscorejs/jQuery 来获取键的准确值吗?
假设您使用的是下划线,请查看有关查找的文档:
http://underscorejs.org/#find
Find 为数组中的每个元素运行一个谓词函数,returns 一个包含谓词函数返回 true 的每个值的新数组。
在这种情况下,我们要查找元素是否具有字段 analytics.demo
为此,我们可以使用下划线函数 _.has
:
http://underscorejs.org/#has
总而言之:
var result = _.find(test, function(elem) {
return _.has(elem, "analytics.demo");
}
);
您可以检查数组中的任何项目是否符合特定条件,如下所示:
var isDemoEnabled = test.some(function (item) {
return item['analytics.demo'] === 'true';
});
if (isDemoEnabled) {
// Do something
}
如果值为布尔值,您可以删除 === 'true'
您可以遍历并检查是否给出了密钥,然后 return 然后检查结果。
function getKeyValue(array, key) {
var value;
array.some(function (a) {
if (key in a) {
value = a[key];
return true;
}
});
return value;
}
var test = [{ "analytics.feature": "false" }, { "demo": "true" }, { "analytics.demo": "false" }]
console.log(getKeyValue(test, 'analytics.demo')); // 'false' a string with this value
const options = Object.assign(...test)
console.log(options) // { "analytics.feature": "false", demo: "true", "analytics.demo": "false" }
if (options['analytics.demo'] === 'true') {
// ...
}
(注意这里使用的是 ES6。对于 ES5,请改用 var options = Object.assign.apply(object, test)
,并且您还需要 polyfill Object.assign
。)
这里使用 Object.assign()
with the spread operator to merge the objects into one, then get the "analytics.demo" option from the new single object. It has to be accessed using bracket notation 因为键中有一个点。它与 string true
进行比较,因为 "false"
(string)为真,但 false
(布尔值)为假。
有关 and converting strings to booleans 的更多信息。
PS 有什么方法可以使 Ajax 调用使用正确的布尔值而不是字符串?使用起来会更容易。
我遇到了一个问题。这是我的 AJAX 电话的回复。
var test = [
{
"analytics.feature": "false"
},
{
"demo": "true"
},
{
"analytics.demo": "false"
}
]
现在我想检查 analytics.demo 是启用还是禁用。
我可以使用 _.find 吗?
_.find(test, {analytics.demo});
//returns 错误
我只想做这个
if(analytics.demo) {
//Do something
}
我该怎么做?有人可以帮忙吗?
除了迭代对象和比较,我可以使用 underscorejs/jQuery 来获取键的准确值吗?
假设您使用的是下划线,请查看有关查找的文档: http://underscorejs.org/#find
Find 为数组中的每个元素运行一个谓词函数,returns 一个包含谓词函数返回 true 的每个值的新数组。
在这种情况下,我们要查找元素是否具有字段 analytics.demo
为此,我们可以使用下划线函数 _.has
:
http://underscorejs.org/#has
总而言之:
var result = _.find(test, function(elem) {
return _.has(elem, "analytics.demo");
}
);
您可以检查数组中的任何项目是否符合特定条件,如下所示:
var isDemoEnabled = test.some(function (item) {
return item['analytics.demo'] === 'true';
});
if (isDemoEnabled) {
// Do something
}
如果值为布尔值,您可以删除 === 'true'
您可以遍历并检查是否给出了密钥,然后 return 然后检查结果。
function getKeyValue(array, key) {
var value;
array.some(function (a) {
if (key in a) {
value = a[key];
return true;
}
});
return value;
}
var test = [{ "analytics.feature": "false" }, { "demo": "true" }, { "analytics.demo": "false" }]
console.log(getKeyValue(test, 'analytics.demo')); // 'false' a string with this value
const options = Object.assign(...test)
console.log(options) // { "analytics.feature": "false", demo: "true", "analytics.demo": "false" }
if (options['analytics.demo'] === 'true') {
// ...
}
(注意这里使用的是 ES6。对于 ES5,请改用 var options = Object.assign.apply(object, test)
,并且您还需要 polyfill Object.assign
。)
这里使用 Object.assign()
with the spread operator to merge the objects into one, then get the "analytics.demo" option from the new single object. It has to be accessed using bracket notation 因为键中有一个点。它与 string true
进行比较,因为 "false"
(string)为真,但 false
(布尔值)为假。
有关
PS 有什么方法可以使 Ajax 调用使用正确的布尔值而不是字符串?使用起来会更容易。