为什么以下工作?
<something>.stop().animate(
{ 'top' : 10 }, 10
);
Run Code Online (Sandbox Code Playgroud)
虽然这不起作用:
var thetop = 'top';
<something>.stop().animate(
{ thetop : 10 }, 10
);
Run Code Online (Sandbox Code Playgroud)
为了使它更清晰:目前我无法将CSS属性作为变量传递给animate函数.
首先,我使用Cheerio进行DOM访问并使用Node.js进行解析.美好时光.
继承人的情况:
我有一个功能,我需要创建一个对象.该对象使用其键和值的变量,然后返回该单个对象.例:
stuff = function (thing, callback) {
var inputs = $('div.quantity > input').map(function(){
var key = this.attr('name')
, value = this.attr('value');
return { key : value }
})
callback(null, inputs);
}
Run Code Online (Sandbox Code Playgroud)
它输出这个:
[ { key: '1' }, { key: '1' } ]
Run Code Online (Sandbox Code Playgroud)
(.map()返回一个对象数组fyi)
我需要key实际上是来自的字符串this.attr('name').
什么是在Javascript中将字符串指定为键的最佳方法,考虑到我正在尝试做什么?
如何使用变量的值a作为查找属性的键?我希望能够说:b["whatever"]并且这个回报20:
var a = "whatever";
var b = {a : 20}; // Want this to assign b.whatever
alert(b["whatever"]); // so that this shows 20, not `undefined`
Run Code Online (Sandbox Code Playgroud)
我在询问是否有可能在创建过程中b,让它包含"whatever":20而不是a:20变量中的"无论什么".也许eval可以使用?
我希望以下三个关联数组是相同的:
arr1 = { "dynamic":"foo", "bar":"baz" };
key = "dynamic";
arr2 = { key:"foo", "bar":"baz" };
arr3 = {};
arr3[key] = "foo";
arr3["bar"] = "baz";
Run Code Online (Sandbox Code Playgroud)
在上述例子中,arr1和arr3是相同的,但是arr2是不同的.
是否可以在javascript关联数组的声明中使用动态键?
所以我想创建一个通用动作创建器来管理redux商店中的项目.
假设我的redux商店看起来像这样:
one: '',
two: '',
three: ''
Run Code Online (Sandbox Code Playgroud)
我的动作创建者看起来像这样:
export const setStatus = (name, status) =>{
return function(dispatch){
dispatch({
type: 'SET_STATUS',
name,
status
})
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何创建一个可以管理所有这些的动作.试着
case 'SET_STATUS':{
return{
...state,
action.name: action.status
}
}
Run Code Online (Sandbox Code Playgroud)
但是redux不会让我.有没有解决方法?
javascript ×5
dictionary ×1
jquery ×1
object ×1
properties ×1
reactjs ×1
redux ×1
variables ×1