var arr = new Map([
['key1', 'value1'],
['key2', 'value2'],
['key3', 'value3']
]);
const data3 = [
{ x: 1, y: 2, z: 3 },
{ x: 4, y: 5, z: 6 },
{ x: 7, y: 8, z: 9 },
];
const dataNew = [
{ a: 1, b: 2, c: 3 },
{ a: 4, b: 5, c: 6 },
{ a: 7, b: 8, c: 9 },
];
{ '0': 'a', '1': 'b', '2': 'c' }
{ '0': 'a', '1': 'b', '2': 'c' }
{ '0': 'a', '1': 'b', '2': 'c' }
data3.reduce(
//(accumulator, currentValue, currentIndex, array) => Object.assign(Object.values(accumulator), Object.values(currentValue)),
(accumulator, currentValue, currentIndex, array) =>
console.log(
Object.fromEntries(
//Object.entries(Object.keys(currentValue), Object.values(currentValue))
Object.entries(["a", "b", "c"], Object.values(currentValue))
)
),
["a", "b", "c"]
);
console.table(data3);
const data1 = [
{ x: 1, y: 2, z: 3 },
//{ x: 4, y: 5, z: 6 },
//{ x: 7, y: 8, z: 9 },
];
const data2 = [
{ x: 1, y: 2, z: 3 },
{ x: 4, y: 5, z: 6 },
//{ x: 7, y: 8, z: 9 },
];
const data3 = [
{ x: 1, y: 2, z: 3 },
{ x: 4, y: 5, z: 6 },
{ x: 7, y: 8, z: 9 },
];
const dataNew = [
{ a: 1, b: 2, c: 3 },
{ a: 4, b: 5, c: 6 },
{ a: 7, b: 8, c: 9 },
];
class DataFrame {
constructor(data, index, column, dtype, name) {
this.data = data;
this.index = index;
this.column = column;
this.dtype = dtype;
this.name = name;
}
get indexs() {
this.index = Object.keys(this.data);
console.log(`index=[${this.index}]`);
return this.index;
}
set indexs(value) {
this.index = value;
console.log(`index=[${this.index}]`);
}
get columns() {
this.column = Array.isArray(this.data)
? [...new Set(this.data.map((k) => Object.keys(k)).flat())]
: this.data;
console.log(`column=[${this.column}]`);
return this.column;
}
set columns(value) {
this.column = Array.isArray(value) ? value : {};
console.log(`column=[${this.column}]`);
}
}
function dataFrame(data, index, column, dtype, name) {
return new DataFrame(data, index, column, dtype, name);
}
const df1 = dataFrame(data3);
//df1.columns;
//df1.columns = ["a", "b"];
//df1.indexs;
//df1.indexs = ['a', 'b', 'c']
//console.table(df1.data);
/*
const i = data3.map((element, index, array) =>
//Object.keys(['a', 'b', 'c']) = Object.values(element)
//{a, b, c} = [1, 2, 3]//...Object.values(element)
//Object.fromEntries(Object.entries(Object.keys(element), Object.values(element)))
//Object.keys(element)
//index
//[{a : Object.values(element)[index]}]
Object.values(element)
//{ a: 1, b: 2, c: 3 }
//Object.assign(['a', 'b', 'c'], Object.values(element))
);
*/
data3.reduce(
//(accumulator, currentValue, currentIndex, array) => Object.assign(Object.values(accumulator), Object.values(currentValue)),
(accumulator, currentValue, currentIndex, array) =>
console.log(
Object.fromEntries(
//Object.entries(Object.keys(currentValue), Object.values(currentValue))
Object.entries(["a", "b", "c"], Object.values(currentValue))
)
),
["a", "b", "c"]
);
console.table(data3);
//console.log(i);
{ 'старый ключ': 'новый ключ' }
:const keys = {
x: 'a',
y: 'b',
z: 'c',
};
const renameKeys = (obj, keys) =>
Object.fromEntries(Object
.entries(obj)
.map(([ k, v ]) => [ Object.hasOwn(keys, k) ? keys[k] : k, v ])
);
// или
const renameKeys = (obj, keys) =>
Object.keys(obj).reduce((acc, k) => (
acc[keys[k] ?? k] = obj[k],
acc
), {});
const newArr = arr.map(n => renameKeys(n, keys));
function renameKeys(keys, obj) {
for (const k in keys) {
if (obj.hasOwnProperty(k)) {
obj[keys[k]] = obj[k];
delete obj[k];
}
}
}
arr.forEach(renameKeys.bind(null, keys));
const data3 = [
{ x: 1, y: 2, z: 3 },
{ x: 4, y: 5, z: 6 },
{ x: 7, y: 8, z: 9 },
];
const changeKeyses = (arr, new_keyses) => arr.map(obj => {
Object.keys(obj).forEach((key, i) => {
let new_key = new_keyses[i];
if (new_key !== undefined) {
let val = obj[key];
delete obj[key];
obj[new_key] = val;
}
});
return obj;
});
console.log(changeKeyses(data3, ["a", "b", "c"]));
console.log(changeKeyses(data3, ["a", "b"]));
console.log(changeKeyses(data3, ["a"]));