过滤后为什么数组为空?

时间:2017-02-21 00:09:15

标签: javascript arrays node.js

我试图过滤数组中的某些对象。但是当我这样做时,我只得到一个空数组。

代码:

let guilds = guildsData.filter((el) => {
    return el.owner == 'true';
});
console.log(guilds);

阵列:

[ 
  { owner: false,
    permissions: 2146958463,
    icon: 'e568d2b87e31358588cb982354628d51',
    id: '267920024570691586',
    name: 'Hydra' },
  { owner: true,
    permissions: 2146958463,
    icon: null,
    id: '269159705794838529',
    name: 'test 2' } ]

(我删除了大部分对象,但它们看起来都像这样)

2 个答案:

答案 0 :(得分:5)

字符串'true'与布尔常量true不同。

==的一边是布尔值而另一边是其他边时,比较是在将该布尔值转换为数字后完成的。因此

'true' == true

执行
'true' == 1

答案 1 :(得分:1)

试试这个:

$('.parent-element-selector-here').on('click', '.fa-unlock-alt, .fa-lock', function () {

    var $button = $(this);

    if ($button.hasClass('fa-unlock-alt')) {

        // do this

    } else {

        // do that 

    }

});

无需将class GameScene: SKScene, SKPhysicsContactDelegate { var health = SKSpriteNode() var healthTimer = Timer() var progressValue = 350 func startHealthTimer() { progressValue += 20 } override func didMove(to view: SKView) { healthTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.startHealthTimer), userInfo: nil, repeats: true) } override func update(_ currentTime: TimeInterval) { let healthTexture = SKTexture(imageNamed: "health.png") health = SKSpriteNode(color: .white, size: CGSize(width: progressValue, height: 30)) health.position = CGPoint(x: -self.frame.width / 2 + healthTexture.size().width / 2, y: self.frame.height / 2) health.zPosition = 2 self.addChild(health) } let guilds = guildsData.filter((el) => { return el.owner; }); 进行比较(无论如何它们都不相同)。