使用javascript中的for循环创建数组对象

时间:2018-06-07 07:57:30

标签: javascript

我有以下数据

 var counts = ["2017-01-01T00:00:00Z", 0, "2017-02-01T00:00:00Z", 0, "2017-03-01T00:00:00Z", 0, "2017-04-01T00:00:00Z", 0, "2017-05-01T00:00:00Z", 0, "2017-06-01T00:00:00Z", 0, "2017-07-01T00:00:00Z", 0, "2017-08-01T00:00:00Z", 0, "2017-09-01T00:00:00Z", 0, "2017-10-01T00:00:00Z", 0, "2017-11-01T00:00:00Z", 0, "2017-12-01T00:00:00Z", 0, "2018-01-01T00:00:00Z", 0, "2018-02-01T00:00:00Z", 0, "2018-03-01T00:00:00Z", 0, "2018-04-01T00:00:00Z", 18, "2018-05-01T00:00:00Z", 482, "2018-06-01T00:00:00Z", 272];

我正在尝试下面的代码来获得我的预期结果,但遗憾的是没有得到结果。

var result = [];
for (i = 0; i < counts.length; i++) {
    console.log(counts[i]);
    resultObj = {"date":counts[i],"post":counts[i+1]};
    result.push(resultObj);
}

预期结果

var finalResults = [{"date":"2017-01-01T00:00:00Z",post:0},{"date":"2017-02-01T00:00:00Z",post:0},{"date":"2017-03-01T00:00:00Z",post:0},{"date":"2017-04-01T00:00:00Z",post:0},{"date":"2017-05-01T00:00:00Z",post:0},{"date":"2017-06-01T00:00:00Z",post:0},{"date":"2017-07-01T00:00:00Z",post:0},{"date":"2017-08-01T00:00:00Z",post:0},{"date":"2017-09-01T00:00:00Z",post:0},{"date":"2017-10-01T00:00:00Z",post:0},{"date":"2017-11-01T00:00:00Z",post:0},{"date":"2017-12-01T00:00:00Z",post:0},{"date":"2018-01-01T00:00:00Z",post:0},{"date":"2018-02-01T00:00:00Z",post:0},{"date":"2018-03-01T00:00:00Z",post:0},{"date":"2018-04-01T00:00:00Z",post:18},{"date":"2018-05-01T00:00:00Z",post:482},{"date":"2018-06-01T00:00:00Z",post:272}]

&#13;
&#13;
var counts = ["2017-01-01T00:00:00Z", 0, "2017-02-01T00:00:00Z", 0, "2017-03-01T00:00:00Z", 0, "2017-04-01T00:00:00Z", 0, "2017-05-01T00:00:00Z", 0, "2017-06-01T00:00:00Z", 0, "2017-07-01T00:00:00Z", 0, "2017-08-01T00:00:00Z", 0, "2017-09-01T00:00:00Z", 0, "2017-10-01T00:00:00Z", 0, "2017-11-01T00:00:00Z", 0, "2017-12-01T00:00:00Z", 0, "2018-01-01T00:00:00Z", 0, "2018-02-01T00:00:00Z", 0, "2018-03-01T00:00:00Z", 0, "2018-04-01T00:00:00Z", 18, "2018-05-01T00:00:00Z", 482, "2018-06-01T00:00:00Z", 272];

var result = [];
for (i = 0; i < counts.length; i++) {
  console.log(counts[i]);
  resultObj = {
    "date": counts[i],
    "post": counts[i + 1]
  };
  result.push(resultObj);
}

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

2 个答案:

答案 0 :(得分:3)

尝试以下方法:

var counts = ["2017-01-01T00:00:00Z", 0, "2017-02-01T00:00:00Z", 0, "2017-03-01T00:00:00Z", 0, "2017-04-01T00:00:00Z", 0, "2017-05-01T00:00:00Z", 0, "2017-06-01T00:00:00Z", 0, "2017-07-01T00:00:00Z", 0, "2017-08-01T00:00:00Z", 0, "2017-09-01T00:00:00Z", 0, "2017-10-01T00:00:00Z", 0, "2017-11-01T00:00:00Z", 0, "2017-12-01T00:00:00Z", 0, "2018-01-01T00:00:00Z", 0, "2018-02-01T00:00:00Z", 0, "2018-03-01T00:00:00Z", 0, "2018-04-01T00:00:00Z", 18, "2018-05-01T00:00:00Z", 482, "2018-06-01T00:00:00Z", 272];

var result = [];
for(var i = 0; i < counts.length; i+= 2){
  var obj = {};
  obj.date = counts[i];
  obj.post = counts[i+1];
  result.push(obj);
}
console.log(result);

答案 1 :(得分:0)

使用此

&#13;
&#13;
class TabBarCollectionView: CollectionView {

   override func setupUI() {
      isSelectable = true
      allowsMultipleSelection = false
      allowsEmptySelection = false
      backgroundView = View(backgroundColor: .magenta)
      backgroundColors = [.clear]
   }
}

class TabBarScrollView: ScrollView {

   override func setupUI() {
      borderType = .noBorder
      backgroundColor = .clear
      drawsBackground = false

      horizontalScrollElasticity = .none
      verticalScrollElasticity = .none

      automaticallyAdjustsContentInsets = false
      horizontalScroller = InvisibleScroller()
   }
}

// Disabling scroll view indicators.
// See: https://stackoverflow.com/questions/9364953/hide-scrollers-while-leaving-scrolling-itself-enabled-in-nsscrollview
private class InvisibleScroller: Scroller {

   override class var isCompatibleWithOverlayScrollers: Bool {
      return true
   }

   override class func scrollerWidth(for controlSize: NSControl.ControlSize, scrollerStyle: NSScroller.Style) -> CGFloat {
      return CGFloat.leastNormalMagnitude // Dimension of scroller is equal to `FLT_MIN`
   }

   override func setupUI() {
      // Below assignments not really needed, but why not.
      scrollerStyle = .overlay
      alphaValue = 0
   }
}

class TabBarTabViewItem: CollectionViewItem {

   private lazy var titleLabel = Label().autolayoutView()

   override var isSelected: Bool {
      didSet {
         if isSelected {
            titleLabel.font = Font.semibold(size: 10)
            contentView.backgroundColor = .red
         } else {
            titleLabel.font = Font.regular(size: 10.2)
            contentView.backgroundColor = .blue
         }
      }
   }

   override func setupUI() {
      view.addSubviews(titleLabel)
      view.wantsLayer = true
      titleLabel.maximumNumberOfLines = 1
   }

   override func setupDefaults() {
      isSelected = false
   }

   func configure(title: String) {
      titleLabel.text = title
      titleLabel.textColor = .white
      titleLabel.alignment = .center
   }

   override func setupLayout() {
      LayoutConstraint.withFormat("|-[*]-|", titleLabel).activate()
      LayoutConstraint.withFormat("V:|-(>=4)-[*]", titleLabel).activate()
      LayoutConstraint.centerY(titleLabel).activate()
   }
}

class TabContentController: ViewController {

   let content: String
   private lazy var titleLabel = Label().autolayoutView()

   init(content: String) {
      self.content = content
      super.init()
   }

   required init?(coder: NSCoder) {
      fatalError()
   }

   override func setupUI() {
      contentView.addSubview(titleLabel)
      titleLabel.text = content
      contentView.backgroundColor = .green
   }

   override func setupLayout() {
      LayoutConstraint.centerXY(titleLabel).activate()
   }
}
&#13;
&#13;
&#13;