最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Javascript for loop to fill in object - Stack Overflow

programmeradmin4浏览0评论

I have an array of domains: var domains = ["domain1", "domain2", "domain3"];

Expected result should be:

var domainData = {
  type: 'bo',
  name: 'domain',
  width: 200,
  offsetLeft: 30,
  label: 'Test label',
  required: true,
  options: [
    {text: 'domain1', value: 'domain1'},
    {text: 'domain2', value: 'domain2'},
    {text: 'domain3', value: 'domain3'},
  ]
};

This is what I was trying to do, but it doesn't work:

for(var i = 0; i < domains.length; i++) {
  console.log(domains[i]);
  var domainData = {
    type: 'bo',
    name: 'domain',
    width: 200,
    offsetLeft: 30,
    label: 'Test label',
    required: true,
    options: [{
      text: domains[i],
      value: domains[i],
    }]
  };
}

You can play with code here: ,console

I have an array of domains: var domains = ["domain1", "domain2", "domain3"];

Expected result should be:

var domainData = {
  type: 'bo',
  name: 'domain',
  width: 200,
  offsetLeft: 30,
  label: 'Test label',
  required: true,
  options: [
    {text: 'domain1', value: 'domain1'},
    {text: 'domain2', value: 'domain2'},
    {text: 'domain3', value: 'domain3'},
  ]
};

This is what I was trying to do, but it doesn't work:

for(var i = 0; i < domains.length; i++) {
  console.log(domains[i]);
  var domainData = {
    type: 'bo',
    name: 'domain',
    width: 200,
    offsetLeft: 30,
    label: 'Test label',
    required: true,
    options: [{
      text: domains[i],
      value: domains[i],
    }]
  };
}

You can play with code here: https://jsbin./vadered/edit?js,console

Share Improve this question edited Feb 2, 2020 at 18:42 Mihai Alexandru-Ionut 48.4k14 gold badges105 silver badges132 bronze badges asked Aug 14, 2018 at 13:45 RolandRoland 3121 gold badge5 silver badges17 bronze badges 1
  • 4 because you keep recreating the parent object and not the child object – epascarello Commented Aug 14, 2018 at 13:47
Add a ment  | 

7 Answers 7

Reset to default 7

Move the domainData object outside your function loop, then add the values for options:

var domainData = {
    type: 'bo',
    name: 'domain',
    width: 200,
    offsetLeft: 30,
    label: 'Test label',
    required: true,
    options: []
};

for(var i = 0; i < domains.length; i++) {
    domainData.options.push({ 'text':domains[i], 'value': domains[i] });
}

The issue is that you're creating every time a new domainData object using the for loop.

You can use map method by passing a callback provided function which is applied for every item from your given array.

var domains = ["domain1", "domain2", "domain3"];
var domainData = {
  type: 'bo',
  name: 'domain',
  width: 200,
  offsetLeft: 30,
  label: 'Test label',
  required: true,
  options: domains.map(elem=> ({text:elem, value:elem}))
};

console.log(domainData);

You do not need a loop for that, since you actually have only 1 object.

What you need is to transform your domain array to options inside your other object. Try this:

var domainData = {
    type: 'bo',
    name: 'domain',
    width: 200,
    offsetLeft: 30,
    label: 'Test label',
    required: true,
    options: domains.map(function(d){ return {text: d, value: d}})
  };

Issue you have is you are recreating the parent object on each iteration and not appending to the child array

So create the child array and use that when you create the object.

var domains = ["domain1", "domain2", "domain3"];

var options = domains.map(function(domain) {
  return {
    text: domain,
    value: domain
  }
})

// without map
// var options = [];
// for (var i=0; i<domains.length; i++) {
//   options.push({text: domains[i], value: domains[i]}
// }

var domainData = {
  type: 'bo',
  name: 'domain',
  width: 200,
  offsetLeft: 30,
  label: 'Test label',
  required: true,
  options: options
};

console.log(domainData)

i played a little bit with your code and ended up with this:

var domains = ["domain1", "domain2", "domain3"];
var optionsBis = [];
for(var i = 0; i < domains.length; i++) {
    optionsBis.push({
      text: domains[i],
      value: domains[i]
    })
}

var domainData = {
  type: 'bo',
  name: 'domain',
  width: 200,
  offsetLeft: 30,
  label: 'Test label',
  required: true,
  options:optionsBis
};

console.log(domainData)

Hope it helps

var domains = ["domain1", "domain2", "domain3"];

var domainData = {
    type: 'bo',
    name: 'domain',
    width: 200,
    offsetLeft: 30,
    label: 'Test label',
    required: true,
    options: []
 };

domains.forEach(domain => {
  domainData.options.push({
    text: domain,
    value: domain
  })
});

console.log(domainData);

You need to remove the main content of domainData out of iterator, and push domains to options, like this:

var domainData = {
  type: 'bo',
  name: 'domain',
  width: 200,
  offsetLeft: 30,
  label: 'Test label',
  required: true,
  options: []
 };

domains.forEach(domain => {
  domainData.options.push({
    text: domain,
    value: domain
  })
});

It's better to create a new copy of object domainData, because of possible nested values.

var domains = ["domain1", "domain2", "domain3"];
var domainData = {
    type: 'bo',
    name: 'domain',
    width: 200,
    offsetLeft: 30,
    label: 'Test label',
    required: true,
  };
var newObjectData  = Object.assign( {}, domainData, {options: domains.map((val) => ({text: val, value: val}))} );
发布评论

评论列表(0)

  1. 暂无评论