1

I've created online cluster in mongodb atlas then created mongodb stitch app, in the stitch javascript editor, how can I get the index of x when match it ? example :

{
"_id": 5646546,
"items":[   {"x": 12, "y": 4} ,  {"x": 12, "y": 4}   ]

}

so when x= 12 the index should be 0

Manaf A.B
  • 27
  • 6
  • So you wanted to write a javascript func ? – whoami - fakeFaceTrueSoul Mar 21 '20 at 20:30
  • Yes, I use python so I know a little about javascript – Manaf A.B Mar 21 '20 at 20:39
  • To explain more , I imported the collection using stitch to goole sheet and everything is ok what I want know is making the changes in google sheet synced to my database SO I'm gonna make google app script that return the id and changes > on the stitch side I'm gonna search for id which is the (x) in my example and update y – Manaf A.B Mar 21 '20 at 20:45

1 Answers1

0

You can write a javascript function in stitch like below :

const getIndex = (inputArray, match) => {
  return inputArray.findIndex(element => element.x == match);
};

let obj = {
  _id: 5646546,
  items: [
    { x: 12, y: 4 },
    { x: 12, y: 4 }
  ]
};

let obj2 = {
  _id: 5646547,
  items: [
    { x: 121, y: 4 },
    { x: 12, y: 4 },
    { x: 122, y: 4 }
  ]
};

console.log(getIndex(obj.items, 12)); // prints 0
console.log(getIndex(obj2.items, 122)); // prints 2
whoami - fakeFaceTrueSoul
  • 17,086
  • 6
  • 32
  • 46