在每个内部创建对象及其对象属性

时间:2017-03-27 09:46:11

标签: javascript arrays

您好我想从已经有名字的对象的数组值创建属性。我的意思是我不想推送像push({date:// whatever})这样的匿名对象。

这是我的代码:



var obj= {}
var arrayObjects= []

var dates= ["Mar 02, 2017", "Mar 02, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017"]


for(var i=0; i< dates.length; i++){

  arrayObjects.push( obj["date"]= dates[i])
}
&#13;
&#13;
&#13;

但我只是进入了arrayObjects日期:

&#13;
&#13;
["Mar 02, 2017", "Mar 02, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017"]
&#13;
&#13;
&#13;

我想要的是一个obj数组,其中包含数组中的每个日期,如:

&#13;
&#13;
[Object {date: "Mar 01, 2017"}, Object {date: "Mar 02, 2017"}] //...
&#13;
&#13;
&#13;

6 个答案:

答案 0 :(得分:2)

您可以生成对象并将它们推送到结果集。

&#13;
&#13;
var arrayObjects = [],
    dates= ["Mar 02, 2017", "Mar 02, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017"],
    i;

for (i = 0; i < dates.length; i++) {
    arrayObjects.push({ date: dates[i] });
}

console.log(arrayObjects);
&#13;
&#13;
&#13;

或使用Array#map

&#13;
&#13;
var dates = ["Mar 02, 2017", "Mar 02, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017"],
    arrayObjects = dates.map(function (a) {
        return { date: a };
    });

console.log(arrayObjects);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

不要忘记分号;。此外,您不必使用obj

只需使用{date:dates[i]}

即可

var arrayObjects= [];

var dates= ["Mar 02, 2017", "Mar 02, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017"];


for(var i=0; i< dates.length; i++){

  arrayObjects.push( {date: dates[i]});
}
console.log(arrayObjects);

来自标题

使用forEach

var arrayObjects= [];

var dates= ["Mar 02, 2017", "Mar 02, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017"];

dates.forEach(x=> arrayObjects.push({date:x}));
console.log(arrayObjects);

答案 2 :(得分:0)

使用Array#reduce

var dates = ["Mar 02, 2017", "Mar 02, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017"],
    result = dates.reduce(function(s,a){
      var obj = {};
      obj.date = a;
      s.push(obj);
      return s;
    }, []);
      
    console.log(result);

答案 3 :(得分:0)

您可以使用1返回对象数组。

4

或者使用ES6箭头功能,您也可以这样做。

map()

答案 4 :(得分:0)

您的代码包含:

$ npm install @ionic/app-scripts --save-dev
ionic-hello-world@ /home/louisro/Documents/mobileApps/BoardLine/ionic2_version
├── @ionic/app-scripts@1.2.1 
└── UNMET PEER DEPENDENCY typescript@2.0.9

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN @ionic/app-scripts@1.2.1 requires a peer of typescript@~2.2.1 but none was installed.


$ ionic info

Your system information:

Cordova CLI: 6.5.0 
Ionic Framework Version: 2.2.0
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.2.0
Ionic App Scripts Version: 1.2.1
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Linux 4.4
Node Version: v7.4.0
Xcode version: Not installed

这会将属性obj.date设置为日期的每个元素,并将指定的字符串值推送到arrayObjects。

使用

arrayObjects.push( obj["date"]= dates[i])

为每个字符串值创建一个对象。 这将使您的初始陈述

arrayObjects.push( { date: dates[i]} )

只要您不在其他地方使用它就过时了......

答案 5 :(得分:0)

您可以使用ES6 for...of方法将dates数组推送元素迭代到arrayObjects

const dates = ["Mar 02, 2017", "Mar 02, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017"];
const arrayObjects = [];

for (d of dates) {
  arrayObjects.push({date: d});
}

console.log(arrayObjects);