1

I have an array of objects. I want to efficiently extract keys and its value out of it.

Example:

let data = [{'Car':'Honda'}, {'car':'Volvo'}];

I want car as key and its values separately. How I can achieve it in efficient manner (even using lodash)?

Expected output will be:

key : Car  value : Honda
key : car  value : Volvo
3
  • 5
    What is expected output? keys and values in which format? in array, in combined object? Commented Jul 12, 2018 at 20:01
  • 2
    What is your expected output? What have you tried so far? We can't help you if you don't show your work. Commented Jul 12, 2018 at 20:01
  • do you mean something like this? data.map(Object.entries); Commented Jul 12, 2018 at 20:13

4 Answers 4

4

You can iterate over the array and access the key and the value of properties for each object in question.

let data = [{
  'Car': 'Honda',
  'hello': 'hi'
}, {
  'car': 'Volvo'
}];

data.forEach((obj) => {
	Object.keys(obj).forEach((key) => {
  		console.log("key : " + key + " - value : " + obj[key]);
  });
});

lodash snippet

let data = [{
  'Car': 'Honda',
  'hello': 'hi'
}, {
  'car': 'Volvo'
}];

_.forEach(data, (obj) => {
	_.forEach(Object.keys(obj), (key) => {
  		console.log("key : " + key + " - value : " + obj[key]);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.core.min.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

this is what i am also doing, but i need something more efficient. Can you give me some example with use of lodash?
1

Object.entries https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

const newArray = data.map((item) => Object.entries(item));
// [[['Car', 'Honda']], [['car', 'Volvo']]]

Comments

1

You can use Object.entries(obj) to iterate over the keys and values.

data.forEach((obj) => 
Object.entries(obj).forEach(([key, value]) => 
console.log(key, value)));

let data = [{
  'Car': 'Honda',
  'hello': 'hi'
}, {
  'car': 'Volvo'
}];

data.forEach((obj) => 
Object.entries(obj).forEach(([key, value]) => 
console.log(key, value)));

Comments

0

Plain and simple way to do it would be:

var data = [{
  'Car': 'Honda'
}, {
  'Car': 'Volvo'
}];

var j = 0;
var carNames= [];
for (var i = 0; i < data.length; i++){
   var nameOfCar = data[i];
   if(nameOfCar.hasOwnProperty('Car')){
      carNames[j] = nameOfCar['Car'];
      j++;
   }
}


console.log(carNames);

3 Comments

I wont call this a 'plain and simple way' though
@aloisdg Why not? OP does not know to loop through an array. To understand for each loop as a novice is difficult, but with for loop it's much easier.
I dont know what OP is looking for and I dont know OP's background.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.