1

I have object array :

enter image description here

{"-LTYJbW1B3mhrdc6C64N":{"done":0,"name":"Job2","pt":5},"-LTYJcSOUh07SQcixP4x":{"done":0,"name":"Job3","pt":5}}

As it seems, there is two objects with random keys (--LTYJbW1B3mhrdc6C64N) , I want to get these KEYS, how I can do that ?

HTML :

<div class="task-block" *ngFor="let task of objectValues(team.tasks)">
 <p >{{task.name}}</p>
 <button mat-raised-button (click)="updateTask(task)">Done!</button>
  <i class="material-icons deletetask">close </i></div>

TS :

objectValues(obj) { 
  if(obj){
    console.log(JSON.stringify(obj))
    return Object.values(obj || {});
  }

  }



updateTask(task){
      console.log(task.key); //here I want to get task.key
    }
4
  • You can use for in loop for object iteration. Commented Dec 12, 2018 at 17:57
  • I tried, as of result I wrote here for an example. Commented Dec 12, 2018 at 17:57
  • Post your code as example in question itself. Commented Dec 12, 2018 at 17:58
  • @AndriusL. Please check the answer. Commented Dec 12, 2018 at 18:06

5 Answers 5

2

html

<div class="task-block" *ngFor="let task of tasks | keyvalue">
  <div (click)="itemClick(task.key)>{{task | json}}</div>
</div>

ts

export class AppComponent  {
  task = {
    "-LTYJbW1B3mhrdc6C64N": {"done":0,"name":"Job2","pt":5},
    "-LTYJcSOUh07SQcixP4x":{"done":0,"name":"Job3","pt":5}
  };

  itemClick(key) {
     console.log(key);
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

How do I get key of an object in TS ?
You missunderstood - i need item key not keyS. I mean one item onClick.
@AndriusL. Check now.
1

Object.keys() is all you need.

let obj = {
"-LTYJbW1B3mhrdc6C64N":{"done":0,"name":"Job2","pt":5},
"-LTYJcSOUh07SQcixP4x":{"done":0,"name":"Job3","pt":5}
}

let op = Object.keys(obj);
console.log(op);

3 Comments

It works - gets two keys, but how to get one when I click on item in ngFor ?
@AndriusL. you can search on object when your onclick event is fired.
I want to get only one key - not all keys. On which item I click - that key i need
1
var keys = Object.keys(YourObject)
console.log(keys)
// ["key1", "keys2",...]


updateTask(task){
   var keys = Object.keys(task)
   console.log(keys[0]);
   return keys[0]
}

MDN Object.keys()

1 Comment

How to get one key ? Of an object I click on ?
0

You can use Object.keys, which will give you an array of keys of the object.

I hope this will solve the issue.

let obj = {"-LTYJbW1B3mhrdc6C64N":{"done":0,"name":"Job2","pt":5},"-LTYJcSOUh07SQcixP4x":{"done":0,"name":"Job3","pt":5}}

let keysOfObj = Object.keys(obj)

console.log("keys of the obj =>", keysOfObj)

3 Comments

How can I get only one object key ? I mean onclick fired. Because I only need one key from an item I click.
Since as you told the key is random, how can we identify is there any identifier to check for which click it has orginated
Okay got it, so you are passing the object only pass the object Id. In the method catch that Id and from the object.keys(obj) use array.find to find the key from the array. Pass obj and Id two params
0

Are these objects also in an array or are they on their own?

If they are in an array you can use this to loop over the object keys which will give there "names" and there sub values

Object.keys(obj).forEach(function(key) {
console.log(key, obj[key]);
});

In your case you just want the first level of names. Just object.keys should do the trick.

console.log(Object.keys(obj));

Source: Javascript object loop

3 Comments

It works, but it gets both values, can I get only one value if I click on one item ? Because as you can see it's in ngFor
@AndriusL. Remove the obj[key] from the console.log, check the updated answer for just object names.
still getting both, not one

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.