javascript - How do you loop over an array of objects and get a key from each? (Angular 2) -


i have array of objects. need loop on array, access same key in each object, , compare string properties array, see if string shares words it. if does: something.

i can compare array have hardcoded string, using lodash _.difference method. i'm confused how string key of each object using loop. tried foreach, can't seem keys of each element.

what best way go achieving this?

thanks much.

component

import { component, oninit, ngzone, viewchild } '@angular/core'; import { routermodule, router } "@angular/router"; import { newsapiservice } '../service/news-api.service'; import { http, response } '@angular/http'; import { mapcomponent } '../map/map.component';  import * _ "lodash";  @component({   selector: 'app-main',   templateurl: './main.component.html',   styleurls: ['./main.component.css'] }) export class maincomponent implements oninit {    private bbcjson: any;   private aljazeerajson: any;   private bingworldjson: any;   private bingpoliticsjson: any;       share(event) {       this.bbcjson.foreach(function(element) {         console.log(element.description);       }) } 

error in console: inline template:0:0 caused by: cannot read property 'foreach' of undefined

assuming have array of objects, array of strings , you're using underscore.js.

here's example ran through interpret question. assumed you're looking intersection on key a:

var arobj = [ { a: "a", b: "b" }, { a: "c", b: "d" } ]; var comparearray = [ "c", "d" ];  var sharedwords = _.intersection(_.pluck(arobj, "a"), comparearray); 

to answer title question - if you're using underscore can _.pluck(array_of_objects, key) array of values.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -