追加为数组内的数组 [英] Append as array inside array

查看:53
本文介绍了追加为数组内的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的对象,可以使用以下方法推送数据。我也初始化 types 数据。

  myObj = {
1:[ a, b, c],
2:[ c, d, e],
}






 数据:{
类型: {}
},

方法:{

pushValue(key,value){
var obj = this.types

if(obj.hasOwnProperty(key)){
var idx = $ .inArray(value,obj [key]);
if(idx == -1){
obj [key] .push([value]);
}
} else {
this。$ set(obj,key,[value]);
}
},
}

工作正常。 / p>




但是,现在我希望我的对象看起来像这样:

  myObj = {
1:{
a:[
[],[]
],
b:[
[],[]
],
}
}

如何修改我的append函数以添加像这样的元素?

解决方案

我只是根据我对@senty想要做什么的假设来回答这个问题:



pushValue是一种将数字作为和字符作为 s,并将它们保存到 this.types 中,以及每当 pushValue 被调用, this.types 将具有属性 key 来存储对象, value 作为其键,用于存储包含空数组的数组。如果该数组(包含数组的数组)已经存在,则将另一个空附加到该数组。最终 this.types 看起来像 myObj



因此, pushValue 应该看起来像这样:



  const app = new Vue({el:'#app',数据:{类型:{}},方法:{pushValue(key,value){if(this.types.hasOwnProperty(key)) {if((this.types [key] .hasOwnProperty(value)){const orgValue = this.types [key] [value]; orgValue.push([]); this。$ set(this.types [key],value ,orgValue);} else {this。$ set(this.types [key],value,[[]]);}} else {this。$ set(this.types,key,{[value]:[[] ]});}}}});  

 <脚本src = https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue。 js>< / script>< div id = app> < div> < p> pushValue(键,值)< / p> < button v-on:click = pushValue(1,‘a’)>(1,‘a’)< / button> < button v-on:click = pushValue(1,‘b’)>(1,‘b’)< / button> < button v-on:click = pushValue(1,‘c’)>(1,‘c’)< / button> < button v-on:click = pushValue(2,‘a’)>(2,‘a’)< / button> < button v-on:click = pushValue(2,‘b’)>(2,‘b’)< / button> < button v-on:click = pushValue(2,‘c’)>(2,‘c’)< / button> < / div> < div> {{类型}}< / div> < / div>  


I have an object that looks like this, and I can push data using below method. Also I initialize the types data.

myObj = {
    1: ["a", "b", "c"],
    2: ["c", "d", "e"],
}


data: {
   types: {}
},

methods: {

  pushValue(key, value) {
      var obj = this.types

      if (obj.hasOwnProperty(key)) {
          var idx = $.inArray(value, obj[key]);
          if (idx == -1) {
             obj[key].push([value]);
          }
      } else {
          this.$set(obj, key, [value]);
      }
  },
}

It works fine.


However, now I want my object to look like this:

myObj = {
    1: {
        "a": [
           [],[]
        ], 
        "b": [
           [],[]
        ],
       }
 }

How can I modify my append function to append the elements like this?

解决方案

I'm just answering this according my assumption of what @senty is trying to do:

pushValue is a method that takes numbers as keys and characters as values and save them into this.types, and whenever pushValue is called, this.types is gonna have a property key storing an object with value as its key, which stores an array containing an empty array. If That array (the one that contains arrays) already exists, another empty is gonna appended to that array. And eventually this.types will look like myObj

Hence, pushValue should look like this:

const app = new Vue({
  el: '#app',
  data: {
    types: {}
  },
  methods: {
    pushValue(key, value) {
      if (this.types.hasOwnProperty(key)) {
        if (this.types[key].hasOwnProperty(value)) {
          const orgValue = this.types[key][value];
          orgValue.push([]);
          this.$set(this.types[key], value, orgValue);
        } else {
          this.$set(this.types[key], value, [[]]);
        }
      } else {
        this.$set(this.types, key, {
          [value]: [ [] ]
        });
      }
    }
  }
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.js"></script>
<div id="app">
  <div>
    <p>pushValue(key, value)</p>
    <button v-on:click="pushValue(1, 'a')">(1, 'a')</button>
    <button v-on:click="pushValue(1, 'b')">(1, 'b')</button>
    <button v-on:click="pushValue(1, 'c')">(1, 'c')</button>
    <button v-on:click="pushValue(2, 'a')">(2, 'a')</button>
    <button v-on:click="pushValue(2, 'b')">(2, 'b')</button>
    <button v-on:click="pushValue(2, 'c')">(2, 'c')</button>
  </div>
  <div>{{ types }}</div>  
</div>

这篇关于追加为数组内的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆