js 把数组中每个元素的某个字段取出

8 篇文章 0 订阅
6 篇文章 0 订阅

方法一:map()

let cities =  [
  {city:"北京",id:100000},  
   {city:"上海",id:20000},  
   {city:"广州",id:510000},  
   {city:"深圳",id:518000}];
 let city = cities.map((item) =>{
   return item.city
 });
 console.log(city);

 方法二:foreach()或for()

let cities =  [
  {city:"北京",id:100000},  
   {city:"上海",id:20000},  
   {city:"广州",id:510000},  
   {city:"深圳",id:518000}
];
 let city = []; cities.forEach((item)=>{ 
    city.push(item.city); }); 
console.log('city == ',city);
cartTable.Map(x => {return x.resourceid})                                                                       生成数组
let newArr = cartTable.Map((item, index) => {
return item.resourceid
 var result =cartTable.map(function(item) {
return item.resourceid;
console.log(result)
});
console.log("arr的id集合", newArr)

vue forEach循环数组拿到自己想要的数据

      <el-checkbox v-for="(item) in jurisdictionContent"
                           :label="item.id"
                           :key="item.id"
                           class="checkboxMargin">
                <span>{{item.value}}{{item.checked}}</span>
              </el-checkbox>


      handleJurisdiction(index, row) {//获取权限
            this.selectedCheck=[];
            let me = this;
            this.jurisdiction = true;
            this.roleId = row.id;
            this.$http.get("/role/resources", {
                params: {roleId: this.roleId}
              },
              {
                emulateJSON: true
              }).then((response) => {
              me.jurisdictionContent = response.body;
              me.jurisdictionContent.forEach(function (c) {
                if(c['checked']){
                  me.selectedCheck.push(c.id);
                }
              })
              console.log(this.selectedCheck)
            })

vue 中标签里循环使用v-for ,方法里面循环使用forEach

  // that.tableData=tabled
  // that.tabled.push({'content':[resp.data]})
//   resp.data.(item)=>{tabled.push(item)};
// })

  // forEach(
  //   axios.get('http://localhost:8182/resource/findById/',cartResourceId).then(function (resp) {                                // + _this.cartTable.resourceid调取后端数据,读取到页面相应窗口,+this.$route.query.id
  //         console.log(resp.data)
  //         alert("拿到数据")
  //       }
  //   )
  // )

// _this.tabled
// let tableData = JSON.parse(JSON.stringify(tabled));

  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肆〇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值