1

I have an array like this,

0:{tag_20: "2018051607234047", tag_12: "700", tag_20_credit_n: "529010804376-S"}

based on the above array I want to create new array like this

0:{code: "tag_20", property: "2018051607234047"} 1:{code: "tag_12", property: "700"} 2:{code: "tag_20_credit_n", property: "529010804376-S"}

I'm new this key value concept and angularjs so far I tried with this

var log = []; angular.forEach(values, function(value, key) { this.push('code: ' + key + ' property: ' + value); }, log); 

but getting error.

4
  • 1
    That seems like a funky array, are you sure that is what it looks like? It seems more like an object. Commented Jun 8, 2018 at 3:38
  • What error are you getting? Help us help you. Commented Jun 8, 2018 at 3:39
  • the first one is what im getting as a response from server but i need to create a new one looks like the second one. means tag_20 become a value as well as ''2018051607234047'' become a value under key code and property respectively Commented Jun 8, 2018 at 3:42
  • this is what i get ["code: 0 property: [object Object]"] Commented Jun 8, 2018 at 3:45

1 Answer 1

1

You could take the array of objects and map the objects as new arrays with the wanted key value pairs.

var array = [{ tag_20: "2018051607234047", tag_12: "700", tag_20_credit_n: "529010804376-S" }],
    result = array.map(o => Object
        .entries(o)
        .map(([code, property]) => ({ code, property }))
    );

console.log(result);

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

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.