Published unlisted
Edited
Nov 15, 2020
Insert cell
md`# 20201114 how to change the key of object to a array or another key?`
Insert cell
// require D3 library
d3 = require('d3');
Insert cell
mat = FileAttachment("Book1.csv")
Insert cell
// get the matrix from mat
data = d3.csvParse(await mat.text());
Insert cell
md`## what should I do if I want to change the key 'step' of all these 9 objects to an array like [0,1,2], or to another key, like 'step_2'`
Insert cell
md`---
## kind answers from the users`
Insert cell
// directly modify in the data read sections
data1 = d3.csvParse(await mat.text(), row => {
row['step'] = [1, 2, 3];
return row;
})
Insert cell
updatedData = data.map(d => {
// shallow copy so that we don't modify the objects in `data`, can be used in the main codes
const obj = {...d};
obj['step'] = [0, 1, 2];
return obj;
})
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more