我在javascript中创建一个数组,在其中插入一个元素,然后使用import sys
from PyQt4 import QtCore, QtGui
class Thread(QtCore.QThread):
dataReceived = QtCore.pyqtSignal(str)
def run(self):
# subprocess stuff goes here
for step in range(6):
self.sleep(1)
self.dataReceived.emit('data received: %d' % step)
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.button = QtGui.QPushButton('Start')
self.button.clicked.connect(self.handleButton)
self.setCentralWidget(self.button)
self.thread = Thread(self)
self.thread.dataReceived.connect(self.statusBar().showMessage)
def handleButton(self):
if not self.thread.isRunning():
self.statusBar().showMessage('starting...')
self.thread.start()
def closeEvent(self, event):
self.thread.wait()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = Window()
window.resize(200, 60)
window.show()
sys.exit(app.exec_())
检查它的存在。它返回true。
但是当我使用includes
将数组转换为字符串然后使用JSON.stringify
转换回数组然后测试相同时,它返回false。
令我感到困惑。
JSON.parse
这是一个jsbin。
答案 0 :(得分:0)
当您运行json.parse
时,您将返回一个新对象。使用includes
时,对象按身份而不是值进行比较,这就是返回false的原因。如果您愿意,可以修改includes
原型(虽然我不建议这样做)进行更深层次的对象比较
Array.prototype.includes = function(searchElement /*, fromIndex*/) {
'use strict';
if (this == null) {
throw new TypeError('Array.prototype.includes called on null or undefined');
}
var O = Object(this);
var len = parseInt(O.length, 10) || 0;
if (len === 0) {
return false;
}
var n = parseInt(arguments[1], 10) || 0;
var k;
if (n >= 0) {
k = n;
} else {
k = len + n;
if (k < 0) {k = 0;}
}
var currentElement;
while (k < len) {
currentElement = O[k];
if (JSON.stringify(searchElement) === JSON.stringify(currentElement) || searchElement === currentElement ||
(searchElement !== searchElement && currentElement !== currentElement)) { // NaN !== NaN
return true;
}
k++;
}
return false;
};
请注意,此代码基于Array.prototype.includes
polyfill。
答案 1 :(得分:0)
因为winhowes州包括按身份检查而不是价值。
你可以试试这个:
if (!Array.prototype.includesCallback) {
Array.prototype.includesCallback = function(predicate) {
'use strict';
if (this == null) {
throw new TypeError('Array.prototype.includeCallback called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return true;
}
}
return undefined;
};
}
可以让你利用一个函数来执行comparasin
later.includesCallback (
function(item) {
return item.id === test_object.id;
}
);
答案 2 :(得分:0)
Array.prototype.includesEquivalent = function (obj) {
for (var o in this) {
for (var p in this[o]) {
if (this[o][p] !== obj[p]) {
return false;
}
}
}
return true;
}
var test_object = {'id': 1, 'name': 'Test'};
var initial = [];
initial.push(test_object);
console.log(initial.includes(test_object));
var later = JSON.stringify(initial);
later = JSON.parse(later);
console.log(later.includesEquivalent(test_object));
&#13;