简体   繁体   English

如何从对象数组中获取特定对象并将其推到另一个数组

[英]How to get a particular object from an array of object and push it to an another array

I want to display checktext on paper-dialog with only vehiclename from checkarray and on console i want to display checktext with all the objects from checkarray. 我想在纸形对话框上显示带有来自checkarray的仅车辆名称的Checktext,在控制台上,我想显示带有来自checkarray的所有对象的Checktext。 Currently, on paper-dialog only Truck value comes on clicking any papaer-checkbox 目前,仅在纸质对话中,点击任何纸质复选框即可获得卡车价值

<template is="dom-repeat" items="{{checkdata}}">
    <paper-checkbox on-tap="checkall" checked="{{item.checked}}">{{item.name}}</paper-checkbox>
</template>         

Property: 属性:

checkdata: {
    type: Array,
    value: [{
       name: 'Bike',
       no: 1,
    }, {
       name: 'Car',
       no: 2,
    }, {
       name: 'Cycle',
        no: 3,
    }, {
       name: 'Bus',
       no: 4,
    }, {
       name: 'Truck',
       no: 5,
    }],
}

Javascript: Javascript:

checkall: function() {
    var checkvalue = this.checkdata;
    var checktext = [];
    for (i = 0; i < checkvalue.length; i++) {
        var checkarray = {
            vehiclename: checkvalue[i].name,
            vehiclenumber: checkvalue[i].no,
        };
        if (checkvalue[i].checked == true) {
            checktext.push(checkarray);
        }
    }
    this.checkeditem = checkarray.vehiclename;
    console.log(checktext);
}

I have replied this question pre stage before. 我之前已经回答了这个问题。 Now I could not understand exactly what you really want to do more, hope this will help! 现在我无法确切了解您真正想做的事情,希望对您有所帮助!

 <html> <head> <base href="https://polygit.org/polymer+:master/components/"> <link href="polymer/polymer.html" rel="import"> <link rel="import" href="paper-checkbox/paper-checkbox.html"> <link rel="import" href="paper-button/paper-button.html"> <link rel="import" href="paper-progress/paper-progress.html"> <link rel="import" href="paper-dialog/paper-dialog.html"> <script src="webcomponentsjs/webcomponents-lite.js"></script> </head> <body> <my-test>test</my-test> <dom-module id="my-test"> <template> <style> :host { display: block; height: 100%; } </style> <div> <template is="dom-repeat" items="{{checkdata}}"> <paper-checkbox checked="{{item.checked}}" on-change='checkedChenged'></paper-checkbox> <span>{{item.name}}</span> <br/> <br/> </template> <paper-dialog id="dialog"> <h2>Selected Vehicle: {{vehicle}}</h2> <div class="buttons"> <paper-button dialog-confirm autofocus on-tap='uncheckAll'>Tap me to close</paper-button> </div> </paper-dialog> <paper-button on-tap="checkAll" raised>Check All</paper-button> <paper-button on-tap="uncheckAll" raised>Uncheck All</paper-button> <paper-button on-tap="togglecheckAll" raised> Toggle Check/Uncheck</paper-button> </div> </template> <script> HTMLImports.whenReady(function() { class MyTest extends Polymer.Element { static get is() { return 'my-test'; } static get properties() { return { vehicle:String, checkdata:{ type: Array, value() {return [{name: 'Bike'}, {name: 'Car'}, {name: 'Cycle'}, {name: 'Bus'}, {name: 'Truck'} ]} } }} static get observers() {return [ 'checkCheckdata(checkdata)']} // set this element's employees property constructor() { super(); } checkCheckdata(d){ // console.log('data : ',d) } checkAll() { let checkdata=[]; this.checkdata.forEach((i,x)=> { checkdata.push({"name": i.name, "checked": true}); if (x==this.checkdata.length-1){ this.set('checkdata', checkdata); } }) } uncheckAll() { let checkdata=[]; this.checkdata.forEach((i,x)=> { checkdata.push({"name": i.name, "checked": false}); if (x==this.checkdata.length-1){ this.set('checkdata', checkdata); } }) } togglecheckAll() { let checkdata=[]; this.checkdata.forEach((i,x)=> { checkdata.push({"name": i.name, "checked": !i.checked}); if (x==this.checkdata.length-1){ this.set('checkdata', checkdata); } }) } checkedChenged(i) { this.vehicle= i.model.item.name; this.$.dialog.open(); } } customElements.define(MyTest.is, MyTest); }); </script> </dom-module> </body> </html> 

DEMO (at Codepen) 演示 (在Codepen上)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM