2

please can anyone help me with some code to get specific data from an array here's is my array

var array = [
  {
    "name": "Joe",
    "age": 17
  },
  {
    "name": "Bob",
    "age": 17
  },
  {
    "name": "Carl",
    "age": 35
  }
]

just want to return name and its values without returning age

thanks in advance.

Anuga
  • 2,619
  • 1
  • 18
  • 27
Jayvk1
  • 61
  • 1
  • 5
  • please add what "*fetch specific data*" means. – Nina Scholz Jul 24 '18 at 10:14
  • https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – Teemu Jul 24 '18 at 10:17
  • 1
    Please add an example of what you’d like to get as a result. Do you want an array of numbers or an array of objects with only that one element or only one specific number or... – JJJ Jul 24 '18 at 10:22

7 Answers7

5

Use Array.map() with array destructuring method so that you get only the name key-value. Array.map() will have only one parameter so you do not need to mention return there:

var array = [ {"name":"Joe", "age":17},{"name":"Bob", "age":17},{"name":"Carl", "age": 35}];
var res = array.map(({name}) => ({name}));
console.log(res);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
3

Try using the jQuery.inArray() function. I hope it will help you.

   
    
    function functionname(){
     var names = [ {"name":"Joe", "age":17},{"name":"Bob", "age":17},{"name":"Carl", "age": 35}];
            var name = $('#name').val(); 
            if(jQuery.inArray(name, names)) {  
                alert(name +" Is In Array");  
            } else {  
                alert(name + ' Is NOT In Array...');  
            }  
    }
  
    
   
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="name" id="name"/>
    <button onclick="functionname()" >Submit</button>
Jeet Thaker
  • 150
  • 1
  • 7
3

In case if you want the value only, The following example will give you only names in a 1d array

var array = [ {"name":"Joe", "age":17},{"name":"Bob", "age":17},{"name":"Carl", "age": 35}];
var res = array.map(({name}) => (name));
console.log(res);
sree
  • 3,113
  • 2
  • 18
  • 13
2

You can use Array#map to do that. Destructure the name from the object and return a new object with only the name.

See the working demo below:

var array = [{
  "name": "Joe",
  "age": 17
}, {
  "name": "Bob",
  "age": 17
}, {
  "name": "Carl",
  "age": 35
}];

var result = array.map(({name, ...rest}) => ({name}));
console.log(result);
31piy
  • 23,323
  • 6
  • 47
  • 67
1

Simple enough to access the name attribute of the object you simply need to reference the item in the array and select the name like so:

array[i].name;
Sir Catzilla
  • 321
  • 3
  • 18
1

You could create a helper function to get a by property object off that array :

var persons = [
  {
    "name": "Joe",
    "age": 17
  },
  {
    "name": "Bob",
    "age": 17
  },
  {
    "name": "Carl",
    "age": 35
  }
];

function getElement(personList, property, comparable) {
  var arraySize = personList.length;
  for(var i = 0; i < arraySize; i++) {
     if (personList[i][property] == comparable) {
         return personList[i]
     }
  }
  return {"name":null, "age":null};
}

console.log(getElement(persons, "name", "Joe")); // outputs { "name": "John", "age" : 17}
console.log(getElement(persons, "name", "Fake John")); // outputs { "name": null, "age" :null} 

This way you can retrieve your person object by any property he has.

César Ferreira
  • 681
  • 1
  • 5
  • 12
  • Map uses less code, usually if you want simplicity you go with functional programming, in terms of efficiency, this way is better ;) – César Ferreira Jul 24 '18 at 10:35
  • Why would you not just use the `Array.prototype.find` method for this? `persons.find((person) => person.name === 'Joe')` – fubar Jul 25 '18 at 23:15
-1

All these Array.Map functions facepalm

var array = [
  {
    "name": "Joe",
    "age": 17
  },
  {
    "name": "Bob",
    "age": 17
  },
  {
    "name": "Carl",
    "age": 35
  }
]

array.forEach((entry) => {
  console.log(entry.name) /* Do whatever you want with `entry.name` */
})
Anuga
  • 2,619
  • 1
  • 18
  • 27