-1

I want to update the value of my array of an object matching "key". The find index is working but it's actually pushing the value instead of replacing it?

this.state.data = thedata Array [
  Object {
    "key": "-Lu2u6ib92Ay8Pw1O-3m",
    "question": "1",
  },
  Object {

    "key": "-Lu2u8kZRs2E4X4TgxZd",
    "question": "4",
  },
  Object {

    "key": "-Lu2u7z4--ImiPSkWa1B",
    "question": "3",
  },
this.state.data[this.state.data.findIndex(el => el.key === '-Lu2u7z4--ImiPSkWa1B')] = 'totototot';

Actual output pushes the value to the index but not replacing it:

this.state.data = thedata Array [
  totototot,
  Object {
    "key": "-Lu2u6ib92Ay8Pw1O-3m",
    "question": "1",
  },
  Object {
    "key": "-Lu2u8kZRs2E4X4TgxZd",
    "question": "4",
  },
  Object {
    "key": "-Lu2u7z4--ImiPSkWa1B",
    "question": "3",
  },

Expected output:

this.state.data = thedata Array [
  Object {
    "key": "-Lu2u6ib92Ay8Pw1O-3m",
    "question": "1",
  },
  Object {
    "key": "-Lu2u8kZRs2E4X4TgxZd",
    "question": "4",
  },
  totototot,
ItsPete
  • 2,363
  • 3
  • 27
  • 35
manyouuwx
  • 47
  • 6
  • 1
    That is not valid JSON/JavaScript. Do not use an array of objects, just use a simple key-value map instead. – Mr. Polywhirl Nov 19 '19 at 14:04
  • maybe you can add an example ? ... – manyouuwx Nov 19 '19 at 14:09
  • Do any of these answers provide you with a resolution? If so please accept that answer otherwise provide clear, concise notes as to why not. – Mark Schultheiss Nov 20 '19 at 11:46
  • Your question and your expected output do not match and all your code including the expected results are malformed JavaScript objects. Please clean up the question with functioning code and clarity of your intent. – Mark Schultheiss Nov 20 '19 at 11:58

2 Answers2

1

You can replace the question value for the key '-Lu2u7z4--ImiPSkWa1B' by:

  1. Finding the index
  2. Looking it up in the array
  3. Accessing the desired key of the field to replace OR the entire thing
  4. Replacing its value

let data = [{
  "key": "-Lu2u6ib92Ay8Pw1O-3m",
  "question": "1"
}, {
  "key": "-Lu2u8kZRs2E4X4TgxZd",
  "question": "4"
}, {
  "key": "-Lu2u7z4--ImiPSkWa1B",
  "question": "3"
}];
console.log("Before:",data);
// Where `data` is `this.state.data`
replaceValue(data, 'key', '-Lu2u7z4--ImiPSkWa1B', 'question', 'totototot');
console.log("After:",data);

replaceEntry(data, 'key', '-Lu2u7z4--ImiPSkWa1B',
  // Whatever you want here... (just looks the same)
  {  
    key : '-Lu2u7z4--ImiPSkWa1B',
    question : 'totototot'
  }
);


function replaceValue(data, sourceKey, sourceValue, targetKey, targetValue) {
  data[data.findIndex(el => el[sourceKey] === sourceValue)][targetKey] = targetValue;
}

function replaceEntry(data, sourceKey, sourceValue, targetValue) {
  data[data.findIndex(el => el[sourceKey] === sourceValue)] = targetValue;
}
.as-console-wrapper { top: 0; max-height: 100% !important; }
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • and if i want to change the entir object and not just a target value ? – manyouuwx Nov 19 '19 at 14:14
  • @manyouuwx I will update. The `replaceEntry()` function above is effectively the same as your original code. – Mr. Polywhirl Nov 19 '19 at 14:14
  • i have the same issue. it's not replacing but pushing instead .. – manyouuwx Nov 19 '19 at 14:25
  • 1
    It cannot be pushing, the example above works as intended. It can't possibly be pushing because I did not call `Array.prototype.push`. – Mr. Polywhirl Nov 19 '19 at 16:34
  • actually i found the problem can you read https://stackoverflow.com/questions/58937644/snapshot-key-not-working-with-value-findindex/58938301?noredirect=1#comment104135835_58938301 – manyouuwx Nov 19 '19 at 16:39
  • drive me crazy the findindex not working with snapshot.key of object but work directly when i write it manually but it return me the same value than i write ! – manyouuwx Nov 19 '19 at 16:40
0

The understandable way

const data = [{
  "key": "-Lu2u6ib92Ay8Pw1O-3m",
  "question": "1"
}, {
  "key": "-Lu2u8kZRs2E4X4TgxZd",
  "question": "4"
}, {
  "key": "-Lu2u7z4--ImiPSkWa1B",
  "question": "3"
}];

function replaceItemHavingKeyAByB(array, a, b) {
  return array.map(element => {
    if (element.key === a) {
      return b;
    } else {
      return element;
    }
  });
}

console.log(
  replaceItemHavingKeyAByB(data, "-Lu2u7z4--ImiPSkWa1B", "totototototo")
);

The short way

const data = [{
  "key": "-Lu2u6ib92Ay8Pw1O-3m",
  "question": "1"
}, {
  "key": "-Lu2u8kZRs2E4X4TgxZd",
  "question": "4"
}, {
  "key": "-Lu2u7z4--ImiPSkWa1B",
  "question": "3"
}];

const replaceItemHavingKeyAByB = (array, a, b) => array.map(elt => elt.key === a ? b : elt);    

console.log(
  replaceItemHavingKeyAByB(data, "-Lu2u7z4--ImiPSkWa1B", "totototototo")
);
David Alvarez
  • 1,226
  • 11
  • 23