Questions tagged [array-of-dict]

A sequentially indexed array, in which the elements of the array consist of a mapping of name-value pairs (known as type dict() in Python -- also known as object in Javascript, or associative-array or hash in other programming languages)

Overview

A sequentially indexed array, in which the elements of the array consist of a mapping of name-value pairs (known as type dict() in Python -- also known as object in Javascript, or associative-array or hash in other programming languages)

See also

12 questions
4048
votes
60 answers

Sort array of objects by string property value

I have an array of JavaScript objects: var objs = [ { first_nom: 'Lazslo', last_nom: 'Jamf' }, { first_nom: 'Pig', last_nom: 'Bodine' }, { first_nom: 'Pirate', last_nom: 'Prentice' } ]; How can I sort them by the value of…
Tyrone Slothrop
  • 40,787
  • 3
  • 17
  • 8
2099
votes
36 answers

Find object by id in an array of JavaScript objects

I've got an array: myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}, etc.] I'm unable to change the structure of the array. I'm being passed an id of 45, and I want to get 'bar' for that object in the array. How do I do this in…
thugsb
  • 22,856
  • 6
  • 30
  • 44
1282
votes
17 answers

Get JavaScript object from array of objects by value of property

Let's say I have an array of four objects: var jsObjects = [ {a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}, {a: 7, b: 8} ]; Is there a way that I can get the third object ({a: 5, b: 6}) by the value of the property b for example without a…
user765368
  • 19,590
  • 27
  • 96
  • 167
827
votes
65 answers

How to get distinct values from an array of objects in JavaScript?

Assuming I have the following: var array = [ {"name":"Joe", "age":17}, {"name":"Bob", "age":17}, {"name":"Carl", "age": 35} ] What is the best way to be able to get an array of all of the distinct ages such that I…
Rolando
  • 58,640
  • 98
  • 266
  • 407
648
votes
20 answers

Find a value in an array of objects in Javascript

I know similar questions have been asked before, but this one is a little different. I have an array of unnamed objects, which contain an array of named objects, and I need to get the object where "name" is "string 1". Here is an example array. var…
Arlen Beiler
  • 15,336
  • 34
  • 92
  • 135
199
votes
46 answers

Merge 2 arrays of objects

Lets have a look at an example. var arr1 = new Array({name: "lang", value: "English"}, {name: "age", value: "18"}); var arr2 = new Array({name : "childs", value: '5'}, {name: "lang", value: "German"}); I…
Alexander
  • 2,015
  • 2
  • 13
  • 3
163
votes
17 answers

Compare JavaScript Array of Objects to Get Min / Max

I have an array of objects and I want to compare those objects on a specific object property. Here's my array: var myArray = [ {"ID": 1, "Cost": 200}, {"ID": 2, "Cost": 1000}, {"ID": 3, "Cost": 50}, {"ID": 4, "Cost": 500} ] I'd like…
firedrawndagger
  • 3,573
  • 10
  • 34
  • 51
115
votes
18 answers

Comparing Arrays of Objects in JavaScript

I want to compare 2 arrays of objects in JavaScript code. The objects have 8 total properties, but each object will not have a value for each, and the arrays are never going to be any larger than 8 items each, so maybe the brute force method of…
AdamB
  • 8,850
  • 5
  • 22
  • 14
77
votes
10 answers

Filter array of objects based on another array in javascript

Given an array of objects : people = [ {id: "1", name: "abc", gender: "m", age:"15" }, {id: "2", name: "a", gender: "m", age:"25" }, {id: "3", name: "efg", gender: "f", age:"5" }, {id: "4", name: "hjk", gender: "m", age:"35" }, …
ios_star
  • 781
  • 1
  • 6
  • 5
45
votes
14 answers

Removing duplicate objects (based on multiple keys) from array

Assuming an array of objects as follows: const listOfTags = [ {id: 1, label: "Hello", color: "red", sorting: 0}, {id: 2, label: "World", color: "green", sorting: 1}, {id: 3, label: "Hello", color: "blue", sorting: 4}, {id: 4, label:…
Andy
  • 549
  • 1
  • 6
  • 12
2
votes
4 answers

Finding minimum value in an array of dicts

I have an array like the following: people = [{'node': 'john', 'dist': 3}, {'node': 'mary', 'dist': 5}, {'node': 'alex', 'dist': 4}] I want to compute the minimum of all the 'dist' keys. For instance, in the above example, the…
Vivek Pandey
  • 3,455
  • 1
  • 19
  • 25
-2
votes
3 answers

Printing an array of objects javascript

I am new to Javascript and I was trying to display an array of objects. I could not really make it happen. Because the number of objects in the array is more than one and each object has 2 attributes. eg var person = { firstName : "John", …
Ashrar
  • 1