admin管理员组

文章数量:1289880

I have object array :

{"-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
    }

I have object array :

{"-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
    }
Share Improve this question edited Dec 12, 2018 at 18:01 PokerJoker asked Dec 12, 2018 at 17:55 PokerJokerPokerJoker 2111 gold badge4 silver badges13 bronze badges 4
  • You can use for in loop for object iteration. – Paresh Gami Commented Dec 12, 2018 at 17:57
  • I tried, as of result I wrote here for an example. – PokerJoker Commented Dec 12, 2018 at 17:57
  • Post your code as example in question itself. – Paresh Gami Commented Dec 12, 2018 at 17:58
  • @AndriusL. Please check the answer. – Beginner Commented Dec 12, 2018 at 18:06
Add a ment  | 

5 Answers 5

Reset to default 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);
  }
}

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);

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()

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)

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

本文标签: javascriptAngular 2 how to get key value from object arraysStack Overflow