0

I need to create a string value depending on the length of the array.

var array = ["left", "right", "top", "bottom"];
//list contain array of Object
 list.forEach((po) => {

//for example if the length of the list is 10. i need to generate the string for each loop like this 

// "left0"   for 1 loop i need this value 
// "right0"  for 2 loop i need this value
// "top0"      for 3 loop i need this value
// "bottom0"   for 4 loop i need this value

// "left1"     for 5 loop i need this value
// "right1"   for 6 loop i need this value
// "top1"     for 7 loop i need this value
// "bottom1"     for 8 loop i need this value

// "left2"   for 1 loop i need this value 
// "right2"  for 2 loop i need this value
}

Please help me with this issue. i am finding difficult generating string along with the number at the last and the left, right, top, bottom should be in the order and number should change after 5 loop as shown

2
  • what is the expected result you need Commented Sep 27, 2018 at 10:10
  • for(var i=0; i<list.length; i++){ ... = array[i] + ParseInt(i/array.length); } Commented Sep 27, 2018 at 10:10

3 Answers 3

3

You can do it using the index of list item and length of the array.

var array = ["left", "right", "top", "bottom"];

// Dummy list array
var list = new Array(10).fill(0);

list.forEach((po, i) => {
  // based on index of list calculate position in array
  // by using remainder operator
  var j = i % array.length,
    // get repetion count by simple dividing by the length
    c = Math.floor(i / array.length);
  console.log(array[j] + c)
});

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

Comments

0

Using new ECMAScript objects such as arrow functions and the fill prototype method, along with the new let and const variable types, you can do it like this:

let instructions = ['left', 'right', 'top', 'bottom'];
const ARRAY_LEN = 10;
const DUMMY_ELEM = 0;
let dummyArray = new Array(ARRAY_LEN).fill(DUMMY_ELEM);
dummyArray.map((element, index) => {
    const AUX_INDEX = index % instructions.length;
    console.log(instructions[AUX_INDEX] + Math.floor(index / instructions.length));
})

EDIT: I used map but you could use, of course, forEach or maybe, depending on your specific problem, even the Array.prototype.every method.

Comments

0

In case you just want only one value depending on the array length. This might help you:

var array = ["left", "right", "top", "bottom"];
var list = [1,2,3,4,5,6,7];
var index = (list.length - 1) % array.length;
var postfix = Math.floor(list.length / array.length);
var result = array[index] + postfix;
console.log(result);

In case you need list of strings:

var array = ["left", "right", "top", "bottom"];
var list = [1,2,3,4,5,6,7];
var result = list.map((item, index) => array[index % array.length] + Math.floor(index / array.length));
console.log(result);

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.