1
$\begingroup$

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));
    }
$\endgroup$
1
  • 1
    $\begingroup$ 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 ); $\endgroup$ Commented Oct 29, 2018 at 8:17

1 Answer 1

2
$\begingroup$

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.
$\endgroup$
Sign up to request clarification or add additional context in comments.

4 Comments

this is true. Pls, can you help me now how to on this way change property SHOW to true? For this returned id
check out Array#find. I'd think it is more suitable for this case.
@Thomas thank you, find is first thing I try, but not working for me in this case
@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.

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.