1

in my angularjs app I get JSON object from API. I have an input field, where I can input ID manually, and if an item with this ID exists, I need to change his property. I also store this ID to sessionStorage, so when user refresh page, he get value like before refresh.

Here is json from API where I need to change ON THE FLY property SHOW to true if exist

$scope.tableParams.data = [
 {id: 1015, type: "ssss", show: false},
 {id: 1016, type: "ssss", show: false},
 {id: 1017, type: "ssss", show: false},
 {id: 1018, type: "ssss", show: false}
]

Function for getting input value and store to session storage, and also change SHOW property

$scope.getIndex = function (indexOfRow) { //indexOfRow is passed data from input field
       //here I want to change show to true for passed id 

        sessionStorage.setItem("indexOfOpenedRow", JSON.stringify(indexOfRow));
    }

I try answer from here but not working for me

I also try this, but allways get undefined

function findId(array, id) {
        var i, found, obj;
        for (i = 0; i < array.length; ++i) {
          obj = array[i];
          if (obj.id == id) {
            console.log(obj);
            return obj;
          }
        }
        return undefined; // <= You might consider null or undefined here
      }
$scope.getIndex = function (indexOfRow) { //indexOfRow is passed data from input field
       //here I want to change show to true for passed id 
        angular.forEach($scope.tableParams.data, function(key, value){
            var result = findId(key.id, indexOfRow);
            console.log(result);
        });
        sessionStorage.setItem("indexOfOpenedRow", JSON.stringify(indexOfRow));
    }
Arter
  • 2,224
  • 3
  • 29
  • 66
  • 1
    Your findId function expects an array and an integer as its parameters. `findId(key.id, indexOfRow);` This makes it look like you're passing two integers into it. I would expect something like `findId( $scope.tableParams.data, indexOfRow );` – Shilly Oct 29 '18 at 08:17

1 Answers1

2

Filter is the function you are searching for. As i understood your question by reading your code you want to compare an seperate ID with the ID in the JSON? If I am wrong commend here and I will edit the answer:

function findIDAndSetShowingTrue(array, id) {
    return array.filter(curr=> curr.id === id).map(curr => ({...curr, show: true}));
}
  • The filter function iterates over every child inside the array and gives you the specific value (array.filter).
  • Then you can use this child and compare it like an if statement with some other values (current).
  • In the end you compare current.id and your input id (current.id === id) and return the current object if it's true.
Community
  • 1
  • 1
Jonathan Stellwag
  • 3,843
  • 4
  • 25
  • 50
  • this is true. Pls, can you help me now how to on this way change property SHOW to true? For this returned id – Arter Oct 29 '18 at 08:27
  • check out [Array#find](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find). I'd think it is more suitable for this case. – Thomas Oct 29 '18 at 08:28
  • @Thomas thank you, find is first thing I try, but not working for me in this case – Arter Oct 29 '18 at 08:29
  • 1
    @Arter - now the answer should solve your problem - If you also need the old array objects with the show values == false you need to use the same syntax I am using in the map: (...curr, show: true). This is called the spread operator and with this you can combine two arrays - It works like a Object.assing. – Jonathan Stellwag Oct 29 '18 at 09:04