使用underscorejs我可以过滤对象中的数据,但它不会保留它的键。有没有办法用钥匙取回物品
var data = {
foo: 'fff',
bar: 'sss',
aaa: 'gggg'
}
console.info('returns array with keys:')
console.log(data)
var ret = _.filter(data, function(el, key) {
if (key == 'foo' || el == 'sss') {
return true
}
})
console.info('keys are lost?')
console.log(ret)
答案 0 :(得分:2)
使用过滤器完成了工作:
console.log(data)
var filtered = {};
var ret = _.each(data, function(el, key) {
if ((key == 'foo' || el == 'sss')) {
filtered[key] = el;
}
})
console.info('keys are not lost?')
console.log(filtered)
工作代码here
答案 1 :(得分:0)
这样的事情怎么样(下划线在这里有点没用)?
var canvas = document.getElementById("signatureCanvas");
if (canvas != null) {
var ctx = canvas.getContext("2d");
ctx.font = "12px sans-serif";
ctx.fillStyle = "black";
ctx.textAlign = "center";
ctx.fillText("Signature", canvas.width / 2, canvas.height / 2);
}