0

I have an object of key and values(array of objects)

 Obj:{ 
    key1: [{desc:'aaa', pos:0}, {desc:'bbb', pos:1}],
    key2: [{desc:'ccc', pos:0}, {desc:'ggg', pos:1}],
    key3: [{desc:'ddd', pos:0}, {desc:'jjj', pos:1}],
    key4: [{desc:'eee', pos:0}, {desc:'kkk', pos:1}]
  }

From the above...I need to get an array of objects for each key in the object. So,I need to get the desc property of a position 0 and assign to text1 below. The text2 and text 3 will be hardcoded.

data = [
  {
    text1: 'aaa',
    text2: 'name2',
    text3: '29/112/2017',
  },
  {
    text1: 'ccc',
    text2: 'name2',
    text3: '29/12/2017',
  },
 {
    text1: 'ddd',
    text2: 'name2',
    text3: '29/12/2017',
  },
 {
    text1: 'eee',
    text2: 'name2',
    text3: '29/12/2017',
  },

]

Tried the following but doesn't work...

 var values = Object.values(obj); - would give me an array of array of objects

 values.map((item, index) => {
    item.map((i, x) => {

    if(i.pos == 0)
        {
           return(
               { 
                  text1: i.desc,
                  text2: 'name2'
                  text3: '29/12/2017'

               }
               )
         }
})

How would I get a structure like "data" array?

3 Answers 3

1

There's no need to use item.map. All you care about is item[0], so just index it directly.

var obj = { 
    key1: [{desc:'aaa', pos:0}, {desc:'bbb', pos:1}],
    key2: [{desc:'ccc', pos:0}, {desc:'ggg', pos:1}],
    key3: [{desc:'ddd', pos:0}, {desc:'jjj', pos:1}],
    key4: [{desc:'eee', pos:0}, {desc:'kkk', pos:1}]
  };
  
var values = Object.values(obj);

var result = values.map(item => ({
  text1: item[0].desc,
  text2: 'name2',
  text3: '29/12/2017'
}));

console.log(result);

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

1 Comment

Thanks Barmar and Taimoor!!! I'll go with Taimoor's answer as its checking for position(pos)...
0

As pointed out by Barmar, there is no need to have a separate array because map returns an array. I have fixed your code so you can have an idea of how you can dynamically push data into an array:

data = []
var values = Object.values(Obj)
values.map ((item , index)=>{
    item.map((i , x)=>{
        if(i.pos == 0){
            data.push({ 
                  "text1": i.desc,
                  "text2": 'name2',
                  "text3": '29/12/2017'

             })
        }
    })
})
console.log(data)

Comments

0

var obj = { 
    key1: [{desc:'aaa', pos:0}, {desc:'bbb', pos:1}],
    key2: [{desc:'ccc', pos:0}, {desc:'ggg', pos:1}],
    key3: [{desc:'ddd', pos:0}, {desc:'jjj', pos:1}],
    key4: [{desc:'eee', pos:0}, {desc:'kkk', pos:1}]
  };
 
 var data = [];
  
  Object.keys(obj).forEach(function(key, value){
	var dict = {};
	dict.text1 = obj[key][0].desc;
	dict.text2 = 'name2';
	dict.text3 = '29/112/2017'
	data.push(dict);
});
console.log(data);

Comments

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.