有没有一种方法可以在对象内部动态添加新属性,同时还可以动态添加新属性?

时间:2019-02-07 09:17:23

标签: javascript

我需要一个具有可以动态添加的属性的对象,并且在该属性内还需要动态添加一个对象。

relinquish '$' variable

这就是我想要的

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/jquery-1.6.2.js"></script>
<script>
   jq162 = jQuery.noConflict( true );//This restores 1.10.2 to '$'
   jq162('body').show();//jq162 holds jQuery 1.6.2
</script>

3 个答案:

答案 0 :(得分:1)

您可以检查下面的代码,我们使用映射将数组值移动到对象属性,然后最后将结构重新分配给相同的变量并获得输出。

const paths = ["POOL-p64/ld3/1/eee", "POOL-p64/ld3/1/afp_new"];
const peer = "EVO-Cluster-at-EVO-57";
const values = [
  {peer: "EVO-Cluster-at-EVO-57", path: "POOL-p64/ld3/1/eee", name: "14", isAllocated: false},
  {peer: "EVO-Cluster-at-EVO-57", path: "POOL-p64/ld3/1/afp_new", name: "3", isAllocated: false}
];
let out = {};
paths.map(function(x, i) {
  let value = values.filter(brick => brick.isAllocated === false && brick.path===x);
    out[x] = value ? [values[i]] : []
});
out = {[peer]: out}
console.log(out);

答案 1 :(得分:1)

我想这就是你想要的。

我在原始数组中添加了一些值以查看更多边缘情况。

应过滤掉添加的两个对象(同级:"EVO-Cluster-at-EVO-571"和路径:"POOL-p64/ld3/1/eeea")。

我还添加了两个对象,它们共享相同的对等点和路径,以便将它们推到objectINeeded[<peer>][<path>]的数组中。

结果是一个对象具有一个对等键("EVO-Cluster-at-EVO-57"),该对象具有两个路径键("POOL-p64/ld3/1/eee""POOL-p64/ld3/1/afp_new"),其中第一个具有两个对象,第二个具有只有一个物体。

这只是一个更笼统的答案,因为它允许多个同伴

console.clear()

const paths = [
  "POOL-p64/ld3/1/eee", 
  "POOL-p64/ld3/1/afp_new"
];
const peers = ["EVO-Cluster-at-EVO-57"];
const values = [
  {
    peer: "EVO-Cluster-at-EVO-57", 
    path: "POOL-p64/ld3/1/eeea", 
    name: "14", 
    isAllocated: false
  },
  {
    peer: "EVO-Cluster-at-EVO-571", 
    path: "POOL-p64/ld3/1/eee", 
    name: "14", 
    isAllocated: true
  },
  {
    peer: "EVO-Cluster-at-EVO-57", 
    path: "POOL-p64/ld3/1/eee", 
    name: "14", 
    isAllocated: false
  },
  {
    peer: "EVO-Cluster-at-EVO-57", 
    path: "POOL-p64/ld3/1/eee", 
    name: "15", 
    isAllocated: false
  },
  {
    peer: "EVO-Cluster-at-EVO-57", 
    path: "POOL-p64/ld3/1/afp_new", 
    name: "3", 
    isAllocated: false
  }
];

// Filter the original Array so that only those with the right properties are used
const filtered = values.filter(brick => (brick.isAllocated === false) 
                               && (paths.indexOf(brick.path) !== -1)
                               && (peers.indexOf(brick.peer) !== -1)
                              );

// 'Map' the filteres array to an object where the 'peer' and 'path' values are used as keys
const objectINeeded = filtered.reduce((map, obj, org) => {
  if (!map[obj.peer]) { map[obj.peer] =  {}; }
  if (!map[obj.peer][obj.path]) { map[obj.peer][obj.path] =  []; }
  map[obj.peer][obj.path].push({...obj});
  return map;
}, {});

console.log(objectINeeded)

答案 2 :(得分:0)

尝试此代码。

let objectINeeded2 = {};
objectINeeded2[peer] = {};
paths.forEach((x) => {
    objectINeeded2[peer][x] = values.filter(brick => brick.isAllocated === false && brick.path === x);
})
console.log(objectINeeded2);
相关问题