You can use Object.keys(obj) to get named indexes. This will return an array structure which you can use/customize further. A sample use to iterate over object values may look like this
var persons = {
john: { age: 23, year:2010},
jack: { age: 22, year:2011},
jenny: { age: 21, year:2012}
}
Getting an iterator
var resultArray = Object.keys(persons).map(function(personNamedIndex){
let person = persons[personNamedIndex];
// do something with person
return person;
});
// you have resultArray having iterated objects
Answer from Farooq Ahmed Khan on Stack OverflowYou can use Object.keys(obj) to get named indexes. This will return an array structure which you can use/customize further. A sample use to iterate over object values may look like this
var persons = {
john: { age: 23, year:2010},
jack: { age: 22, year:2011},
jenny: { age: 21, year:2012}
}
Getting an iterator
var resultArray = Object.keys(persons).map(function(personNamedIndex){
let person = persons[personNamedIndex];
// do something with person
return person;
});
// you have resultArray having iterated objects
Since Angular 6, there is now a keyvalue pipe operator. Simple do the following:
*ngFor="let item of objectList | keyvalue"
item.key # refers to the keys (134, 135...) in your example
item.value # refers to the object for each key
Convert Object to Array Typescript
Typescript convert object to Array - Stack Overflow
Typescript: Is there a simple way to convert an array of objects of one type to another - Stack Overflow
How to convert an array into an object?
Just use bracket.
let obj = {0: "item A", 1: "item B", 2: "item C"}
console.log([obj]);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Also, there's an specific Array function for this matter: Array.of:
console.log ( Array.of ( 1 ) )
Run code snippetEdit code snippet Hide Results Copy to answer Expand
This is more functional-friendly:
const pipe = funs => x => funs.reduce( ( r, fun ) => fun ( r ), x )
const append = x => array => [ ...array, x ]
const sum = values => values.reduce ( ( r, value ) => value + r )
const result = pipe ( [
Array.of,
append ( 2 ),
sum
] ) ( 1 )
console.log ( result )
Run code snippetEdit code snippet Hide Results Copy to answer Expand
You haven't showed enough of your code, so I'm not sure how you instantiate your classes, but in any case you can use the array map function:
class Item {
name: string;
desc: string;
meta: string
}
class ViewItem {
name: string;
desc: string;
hidden: boolean;
constructor(item: Item) {
this.name = item.name;
this.desc = item.desc;
this.hidden = false;
}
}
let arr1: Item[];
let arr2 = arr1.map(item => new ViewItem(item));
(code in playground)
Edit
This can be shorter with Object.assign:
constructor(item: Item) {
Object.assign(this, item);
}
you can do use something like this.
const newdata = olddata.map((x) => {
return { id: Number(x.id), label: x.label };
});
as the converted column will be mapped to the newdata array.
ECMAScript 6 introduces the easily polyfillable Object.assign:
The
Object.assign()method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.
Object.assign({}, ['a','b','c']); // {0:"a", 1:"b", 2:"c"}
The own length property of the array is not copied because it isn't enumerable.
Also, you can use ES8 spread syntax on objects to achieve the same result:
{ ...['a', 'b', 'c'] }
For custom keys you can use reduce:
['a', 'b', 'c'].reduce((a, v) => ({ ...a, [v]: v}), {})
// { a: "a", b: "b", c: "c" }
With a function like this:
function toObject(arr) {
var rv = {};
for (var i = 0; i < arr.length; ++i)
rv[i] = arr[i];
return rv;
}
Your array already is more-or-less just an object, but arrays do have some "interesting" and special behavior with respect to integer-named properties. The above will give you a plain object.
edit oh also you might want to account for "holes" in the array:
function toObject(arr) {
var rv = {};
for (var i = 0; i < arr.length; ++i)
if (arr[i] !== undefined) rv[i] = arr[i];
return rv;
}
In modern JavaScript runtimes, you can use the .reduce() method:
var obj = arr.reduce(function(acc, cur, i) {
acc[i] = cur;
return acc;
}, {});
That one also avoids "holes" in the array, because that's how .reduce() works.