简体   繁体   English

分离出对象内部的对象数组(JavaScript)

[英]seperate out an array of objects inside an object (Javascript)

I have a data object. 我有一个数据对象。 If i do console.log(data), here is the output. 如果我做console.log(data),这是输出。

Object {Info: Array[3]}
  > Info: Array[3]
     >[0]: Object
           name: 'Alex'
           sex: 'Male'
           new_joinee: 0
     >[1]: Object
           name: 'Anna'
           sex: 'female'
           new_joinee: 1
     >[2]: Object
           name: 'lester'
           sex: 'Male'
           new_joinee: 1

Now, if i want to access new_joinee, i have to type as follows. 现在,如果我要访问new_joinee,则必须输入以下内容。

data.Info[0].new_joinee 
data.Info[2].sex

Just a few examples mentioned here. 这里仅举几个例子。 I want to eliminate this and instead be able to get the output by just typing 我想消除这一点,而是只需输入即可获取输出

Info[0].new_joinee
Info[2].sex

Can someone let me know how can i achieve this. 有人可以让我知道如何实现这一目标。 (answered by alexander) (由亚历山大回答)

I have one query more on this one. 我对此有一个查询。 Since, we can see new_joinee is either 0 or 1. I want it to be false for 0 and true for 1. So new data should be as follows. 因为,我们可以看到new_joinee为0或1。我希望它对0为false,对1为true。因此,新数据应如下所示。

Info: Array[3]
 >[0]: Object
       name: 'Alex'
       sex: 'Male'
       new_joinee: false
 >[1]: Object
       name: 'Anna'
       sex: 'female'
       new_joinee: true
 >[2]: Object
       name: 'lester'
       sex: 'Male'
       new_joinee: true

I need to dynamically do it. 我需要动态地做到这一点。 is there a way to achieve this ? 有没有办法实现这一目标?

You can assign object property to variable ( var Info = data.Info; ) 您可以将对象属性分配给变量( var Info = data.Info;

 var data = { Info: [ { name: 'Alex', sex: 'Male', new_joinee: 0 }, { name: 'Anna', sex: 'female', new_joinee: 1 } ] }; var Info = data.Info; console.log(Info[0].name); console.log(Info[0].new_joinee); console.log(Info[0].sex); console.log(Info[1].name); console.log(Info[1].new_joinee); console.log(Info[1].sex); 

Javascript arrays are assigned by reference . Javascript数组通过引用 分配 So, if you simply declare a new variable and assign the desired property 因此,如果您只是声明一个新变量并分配所需的属性

var info = data.Info;

voilà, you get a reference to the original property of the object, that you can use directly. 您可以直接使用该对象的原始属性的引用。

console.log(info[2].name,'is',info[2].sex);

For the second question, .map() is your friend 对于第二个问题, .map()是您的朋友

var info=data.Info.map(function(element){
 element.new_joinee=(element.new_joinee!=0);
 return element;
});

Be aware that you have changed the semantics. 请注意,您已更改了语义。 Now, info is a copy of the array, not a reference. 现在, info是数组的副本 ,而不是引用。 Eventhough the objects it contains are still references to the original objects. 即使它包含的对象仍然是对原始对象的引用。

you could do this instead 你可以这样做

   var info = data.Info;
   info.map(function(element){
     element.new_joinee=(element.new_joinee!=0);
     return element;
   });

in this case info and data.Info are references to the same array, the copy that .map() creates is just discarded. 在这种情况下, infodata.Info是对同一数组的引用, .map()创建的副本将被丢弃。 But, because the objects are references to the same objects, the changes in the objects' properties are reflected in the original array. 但是,由于对象是对相同对象的引用,因此对象属性的更改会反映在原始数组中。

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

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