1

I have an object like this:

var Object = {
        "id": "Siplus",
        "name":"Siplus",
        "icon":"forum"
      },
      {
        "id": "Recent",
        "name":"Recent Activities",
        "icon": "restore"
      },
      {
        "id": "jobList",
        "name":"Job List",
        "icon": "briefcase"
      },
      {
        "id": "Favourites",
        "name":"Favourites",
        "icon": "star"
      },
      {
        "id": "searchQuote",
        "name":"Search Quotes",
        "icon": "binoculars"
      },
      {
        "id": "orderStatus",
        "name":"Order Status",
        "icon": "clock"
      };

I have another array Like this

var array = [1,2,3];

I adding array values to object using this code:

for (var i = 0; i < object.length; i++) {
    object[i].number = array[i];
  }

I am getting result like this:

var Object = {
            "id": "Siplus",
            "name":"Siplus",
            "icon":"forum",
            "number":1
          },
          {
            "id": "Recent",
            "name":"Recent Activities",
            "icon": "restore",
            "number":2
          },
          {
            "id": "jobList",
            "name":"Job List",
            "icon": "briefcase",
            "number":3
          },
          {
            "id": "Favourites",
            "name":"Favourites",
            "icon": "star",
            "number":undefined
          },
          {
            "id": "searchQuote",
            "name":"Search Quotes",
            "icon": "binoculars",
            "number":undefined
          },
          {
            "id": "orderStatus",
            "name":"Order Status",
            "icon": "clock",
            "number":undefined
          };

I wanted like this :

var Object = {
            "id": "Siplus",
            "name":"Siplus",
            "icon":"forum",
            "number":1
          },
          {
            "id": "Recent",
            "name":"Recent Activities",
            "icon": "restore",
            "number":2
          },
          {
            "id": "jobList",
            "name":"Job List",
            "icon": "briefcase",
            "number":3
          },
          {
            "id": "Favourites",
            "name":"Favourites",
            "icon": "star",
            "number":1
          },
          {
            "id": "searchQuote",
            "name":"Search Quotes",
            "icon": "binoculars",
            "number":2
          },
          {
            "id": "orderStatus",
            "name":"Order Status",
            "icon": "clock",
            "number":3
          };

Is their any way to get repeat the number instead of getting "undefined"

Please help me for this

1
  • Hi Vinu, I suggest you edit the question with valid arrays (they are missing [ and ]) and you can't use Object as it is a reserved keyword (use object with small o)... Commented Dec 16, 2016 at 14:31

5 Answers 5

2

You could map your input objects by adding the right value from array thanks to the modulo calculus

var data = [{
        "id": "Siplus",
        "name":"Siplus",
        "icon":"forum"
      },
      {
        "id": "Recent",
        "name":"Recent Activities",
        "icon": "restore"
      },
      {
        "id": "jobList",
        "name":"Job List",
        "icon": "briefcase"
      },
      {
        "id": "Favourites",
        "name":"Favourites",
        "icon": "star"
      },
      {
        "id": "searchQuote",
        "name":"Search Quotes",
        "icon": "binoculars"
      },
      {
        "id": "orderStatus",
        "name":"Order Status",
        "icon": "clock"
      }];
var array = [1,2,3];
res = data.map((x,i) => {
  x.number = array[i % array.length]
  return x;
})
console.log(res);

Sign up to request clarification or add additional context in comments.

Comments

1

The size of array is 3 while that of object is more - a solution would be to use:

object[i].number = array[i % array.length];

See demo below:

var object=[{"id":"Siplus","name":"Siplus","icon":"forum"},{"id":"Recent","name":"Recent Activities","icon":"restore"},{"id":"jobList","name":"Job List","icon":"briefcase"},{"id":"Favourites","name":"Favourites","icon":"star"},{"id":"searchQuote","name":"Search Quotes","icon":"binoculars"},{"id":"orderStatus","name":"Order Status","icon":"clock"}]

var array = [1, 2, 3];

for (var i = 0; i < object.length; i++) {
  object[i].number = array[i % array.length];
}

console.log(object);
.as-console-wrapper{top:0;max-height:100%!important;}

1 Comment

@VinuViswambharan could you please respond to the answers? :)
1

you could use an additional var.

for (var i = 0, j = 0; i < object.length; i++) {
    j++
    if(j > array.length){j=0}
    object[i].number = array[j];
}

1 Comment

Thanks all for support.. Its working.. Thanks again!
1
var arrLength = array.length;
for (var i = 0, j = 0; i < object.length; i++, j++) {
    if (i >= arrLength ) {
        j = 0;
    }
    object[i].number = array[j];
}

1 Comment

It's better to add more context/explanation around code (as opposed to just having a code-only answer) as that makes the answer more useful.
0

You could use a temporary value to point to the numbers array like this:

var temp = 0;
for (var i = 0; i < object.length; i++) {
    object[i].number = array[temp];
    if(temp == array.length)
         temp = 0;
    else
         temp++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.