0

I have an array like this;

var specialOne = 3;
var array = [{value:"special"},{value:"1"},{value:"2"},{value:"specialOne"},{value:"4"},{value:"special"}];

And I need to convert it to this array;

var temp = [{value:"0"},{value:"1"},{value:"2"},{value:"3"},{value:"4"},{value:"5"}];

special's should be replaced with the appropriate value.

specialOne should be replaced with the given number.

How can i do this ?

More examples:

0,special,2,special,4,5 => 0,1,2,3,4,5
7,8,9,special => 7,8,9,10
special,special,10 => 8,9,10
1
  • I'm not sure what you want - too confusing. You can do this: for(var i=0; i<array.length; i++) array[i].value=""+i;, but I have a feeling that this is not what you mean/want... Commented Jul 21, 2020 at 11:40

4 Answers 4

1

Maybe this is what you are after

var specialOne = 3;
var array1 = [{value:"special"},{value:"1"},{value:"2"},{value:"specialOne"},{value:"4"},{value:"special"}];
  function addspec(specialOne){
    array1.forEach((o,i)=>{
      if(o.value=="specialOne")o.value = specialOne.toString()   
      if(o.value=="special") o.value = array1[i-1]?(parseInt(array1[i-1].value)+1).toString():"0"
    })
  }
  addspec(3)
console.log(array1)

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

Comments

0

This may help you

var specialOne = 3;
var array = [
  { value: "special" },
  { value: "1" },
  { value: "2" },
  { value: "specialOne" },
  { value: "4" },
  { value: "special" }
];
for (i = 0; i < array.length; i++) {
  if (array[i].value == 'specialOne') {
    array[i].value = String(specialOne);
    console.log(array);
  } else if (array[i].value == 'special') {
    array[i].value = String(0);
    array.pop()
    array.push( { value: String(5) } );
  }
}

Comments

0
for (let i = 0; i < array.length; i++) {
   if (array[i].value === "special") array[i].value = i.toString();
   if (array[i].value === "specialOne") array[i].value = specialOne.toString();  
};

When you need to modify an array you should try to use a 'for' loop first. It is the most efficient as it will not modify the index of the array and will not return a new array (Imagine you have to 10 000 items to modify in your array... ).

Here it's very simple, you itere the array, if the condition match you modify the value (of the array itself).

1 Comment

Please try to elaborate a bit more on why this code works.
0

Something like this?

var specialOne = 3;
var array = [{
  value: "special"
}, {
  value: "1"
}, {
  value: "2"
}, {
  value: "specialOne"
}, {
  value: "4"
}, {
  value: "special"
}];

function getValues(array) {
  let counterSync = 0;
  let backCheck = [];
  let inFlow = false;
  let backCheckOnce = false;
  return array.map((m, i) => {
    if (isNaN(parseInt(m.value))) {
      if (inFlow || window[m.value]) {
        m.value = "" + (window[m.value] || (counterSync + 1));
      } else {
        if (i === 0) {
          backCheckOnce = true;
        }
        backCheck.push(m);
      }
    } else {
      inFlow = true;
      // do reverse check only once
      if (backCheckOnce) {
        backCheck.reverse().forEach((x, idx) => x.value = "" + (+m.value - 1));
        backCheckOnce = false;
      }

    }
    counterSync = +m.value;
    return m;
  });
}

console.log(getValues(array));

3 Comments

No, If start with 5, must be example : 5,6,special,8 => 5,6,7,8
@YavuzSelimÖzmen Updated. The only issue I see is the object should always start with number instead of special or specialOne.
@YavuzSelimÖzmen Hey I fixed the first value as special also. Do understand the code before using it (it can be refactored)

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.