我能够遍历我的JSON对象值并找到我要寻找的字符串,但是我不确定修改后如何设置新值。我可能使用了错误的方法,但可以就如何使其工作以及应该使用哪种适当方法获得一些反馈。
this.specVars = {
rgba_000000 : "rgba(0, 0, 0, 1)", //Black
rgba_222222 : "rgba(34, 34, 34, 1)", //Dark gray
rgba_2ba6cb : "rgba(43, 166, 203, 1)",
rgba_336699 : "rgba(51, 102, 153, 1)", //Blue hyperlink
rgba_ffffff : "rgba(255, 255, 255, 1)", //White
};
//测试使用的浏览器,并在使用Firefox时从颜色值中删除alpha(opacity)值
///将对象中的值转换为用于解析每个值的数组
var colors = this.specVars;
var values = Object.values(colors);
for (var value of values){
if(value.includes(', 1)')){
value = value.replace(', 1)', ')');
value = value.replace('rgba', 'rgb');
//How do i set the value in the actual object??
console.log(value);
};
}
答案 0 :(得分:1)
您可以像这样在任何索引处简单地更改值
this.specVars['rgba_000000'] = 10;
以您的情况
this.specVars = {
rgba_000000 : "rgba(0, 0, 0, 1)", //Black
rgba_222222 : "rgba(34, 34, 34, 1)", //Dark gray
rgba_2ba6cb : "rgba(43, 166, 203, 1)",
rgba_336699 : "rgba(51, 102, 153, 1)", //Blue hyperlink
rgba_ffffff : "rgba(255, 255, 255, 1)", //White
};
var colors = this.specVars;
var values = Object.values(colors);
for (var value of values){
if(value.includes(', 34')){
values[values.indexOf(value)] = 10;
}
}
console.log(values);
或
var keys = Object.keys(colors);
var values = Object.values(colors);
for (var value of values){
if(value.includes(', 34')){
this.specVars[keys[values.indexOf(value)]] = 10;
}
}
答案 1 :(得分:1)
在keys数组上循环。例如:
var colors = this.specVars;
var keys = Object.keys(colors);
keys.forEach(key => {
let value = colors[key];
if(value.includes(', 1)')){
value = value.replace(', 1)', ')');
value = value.replace('rgba', 'rgb');
colors[key] = value;
console.log(value);
};
});
答案 2 :(得分:0)
尝试
---STARTING ATOMIC #### Atomic context wrapping my serializer's create method
Executing query: 'SAVEPOINT `s139667621889792_x1`' - args: None
---STARTING ATOMIC #### Atomic context wrapping my model's save method
Executing query: 'SAVEPOINT `s139667621889792_x2`' - args: None
---INSIDE MODEL SAVE
---LOCKING TABLES
Executing query: 'LOCK TABLES vertex WRITE, edge WRITE
---LOCKED TABLES
---STARTING ATOMIC #### I think Django must wrap some queries in an atomic block, but this doesnt even create a savepoint
Executing query: 'INSERT INTO `edge`...
---EXITING ATOMIC
#### WHERE MY CYCLE CHECK CODE WOULD RUN - not implemented yet
---UNLOCKING TABLES
Executing query: 'UNLOCK TABLES' - args: None
---UNLOCKED TABLES
---EXITING ATOMIC
Executing query: 'RELEASE SAVEPOINT `s139667621889792_x2`' - args: None
Executing query: 'ROLLBACK TO SAVEPOINT `s139667621889792_x2`' - args: None ### WHAT I BELIEVE TO BE THE OFFENDING QUERY
---EXITING ATOMIC
Executing query: 'ROLLBACK TO SAVEPOINT `s139667621889792_x1`' - args: None
Traceback (most recent call last):
File ".../site-packages/django/db/backends/utils.py", line 83, in _execute
return self.cursor.execute(sql)
File ".../site-packages/django/db/backends/mysql/base.py", line 72, in execute
return self.cursor.execute(query, args)
File ".../site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File ".../site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File ".../site-packages/pymysql/connections.py", line 516, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File ".../site-packages/pymysql/connections.py", line 727, in _read_query_result
result.read()
File ".../site-packages/pymysql/connections.py", line 1066, in read
first_packet = self.connection._read_packet()
File ".../site-packages/pymysql/connections.py", line 683, in _read_packet
packet.check_error()
File ".../site-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File ".../site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1305, 'SAVEPOINT s139667621889792_x2 does not exist')