拥有JSON字符串(缩短):
{"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]}
我希望能够根据“短”值调用一个返回“long”值的函数:
喜欢:
var test= 'Say '+get_value("_YES");
我该怎么做?
试过:
function f_lang(short_string) {
var obj = json_string;
var arr = [];
json = JSON.stringify(eval('(' + obj + ')')); //convert to json string
arr = $.parseJSON(json); //convert to javascript array
return arr['line_array'][short_string];
}
没有运气
答案 0 :(得分:1)
使用Array#find
查找包含短值的对象。请注意,IE不支持Array#find
。因此,如果您需要IE支持和/或您正在进行大量此类转换,您应该使用字典方法。
var str = '{"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]}';
var terms = JSON.parse(str);
function get_value(short) {
var term = terms.line_array.find(function(o) {
return o.short === short;
});
//in case the term isn't found, we'll prevent term.long from throwing an error
return term && term.long;
}
var result = get_value('_YES');
console.log(result);

使用字典对象
使用Array#reduce
创建字典并使用它:
var str = '{"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]}';
var terms = JSON.parse(str);
var termsDictionary = terms.line_array.reduce(function(d, t) {
d[t.short] = t.long;
return d;
}, Object.create(null));
function get_value(short) {
return termsDictionary[short]; // you can use this expression without the function of course
}
var result = get_value('_YES');
console.log(result);

答案 1 :(得分:0)
使用正确解析的JSON,您可以使用Array#find
,ES6功能。
function getValue(short_string) {
return (object.line_array.find(a => a.short === short_string) || {}).long;
}
var json_string = '{"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]}',
object = JSON.parse(json_string),
test= 'Say '+getValue("_YES");
console.log(test);
ES5与Array#some
function getValue(short_string) {
var value;
object.line_array.some(function (a) {
if (a.short === short_string) {
value = a.long;
return true;
}
});
return value;
}
var json_string = '{"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]}',
object = JSON.parse(json_string),
test= 'Say '+getValue("_YES");
console.log(test);
答案 2 :(得分:0)
另一种选择:
function f_lang(short_string) {
var obj = {"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]};
for (var i = 0; i < obj['line_array'].length; i++) {
if (obj['line_array'][i]['short'] == short_string) {
return obj['line_array'][i]['long'];
};
}
}
console.log('Say ' + f_lang("_YES"));
答案 3 :(得分:0)
您可以使用Array.Filter
var haystack = {"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]}
console.log(haystack.line_array.filter(e => e.short === '_YES').long)
答案 4 :(得分:0)
请尝试以下代码
override func isApplicableToList(list: [ShoppingItem]) -> Bool {
//should return true if a dicount can be applied based on the supplied list of product ids (i.e. a product must be matched to an offer)
for item in list {
if applicableProductIds.contains(item.productId) {
return true
}
}
// didn't find one
return false
}