Javascript …Array.from输出仅显示数组中的最后一个条目。将JSON转换为新的对象形式
我正在从现有数组中创建一个对象,但是当通过object.fromEntries、object.assign和…array.from运行数组时,我会从数组中重复最后一个元素,但数组中的另一个元素会按预期进行迭代 代码将比我解释的更清楚地显示这一点。我发现这很难解释,因为这对我来说太近了 这个问题的奇怪之处在于,我以前在同一个文件中做过这件事,而且效果很好。当复制代码以在另一个数组上复制它时,它的行为与同一函数的上一个格式副本不同。Console.logging在它被操作的每个点上的数据显示数据是良好的,直到…Array.from的结果 更新:添加了完整代码以帮助复制问题 这是完整的功能/应用程序Javascript …Array.from输出仅显示数组中的最后一个条目。将JSON转换为新的对象形式,javascript,arrays,object,ecmascript-6,Javascript,Arrays,Object,Ecmascript 6,我正在从现有数组中创建一个对象,但是当通过object.fromEntries、object.assign和…array.from运行数组时,我会从数组中重复最后一个元素,但数组中的另一个元素会按预期进行迭代 代码将比我解释的更清楚地显示这一点。我发现这很难解释,因为这对我来说太近了 这个问题的奇怪之处在于,我以前在同一个文件中做过这件事,而且效果很好。当复制代码以在另一个数组上复制它时,它的行为与同一函数的上一个格式副本不同。Console.logging在它被操作的每个点上的数据显示数据是良
import React, { Component } from "react";
import data from "./data/convertedData/POSTCODES FOR DISTRICT_AVRG HOUSE PRICE - Average price.json";
class JSONCreator extends Component {
// makeJson = () => {
// data.map(i => )
// }
// {
// "year":,
// "location":,
// "price":,
// "lat-lon":,
// }
render() {
const newData = data.map(i => {
const j = i.year;
const values = Object.entries(j);
Object.fromEntries = arr =>
Object.assign(
{},
...Array.from(arr, ([a, b]) => ({
year: a,
locationAndPrice: b
}))
);
const obj = Object.fromEntries(values);
console.log('obj1', obj)
return obj;
});
const newNewData = newData.map(i => {
console.log('i.locationAndPrice', i.locationAndPrice)
const j = i.locationAndPrice;
const values = Object.entries(j);
console.log('values', values)
Object.fromEntries = arr =>
Object.assign(
{},
...Array.from(arr, ([a, b]) => ({
location: a,
price: b
}))
);
const obj = Object.fromEntries(values);
console.log('obj', obj);
return obj;
});
// const newArrayHere = newData.map(i => {
// const j = i.locationAndPrice;
// const values = Object.entries(j);
// console.log('values', {...[values]})
// // Object.fromEntries = arr =>
// // Object.assign(
// // {},
// // ...Array.from(arr, (p, l) => ({ p, l }))
// // );
// // const obj = Object.fromEntries(values)
// // return obj;
// });
// console.log(newArrayHere);
return <div>hi</div>;
}
}
export default JSONCreator;
下面是我正在尝试运行的函数。
下面是我将上面的JSON转换为下面的下一个JSON对象的过程
const newData = data.map(i => {
const j = i.year;
const values = Object.entries(j);
Object.fromEntries = arr =>
Object.assign(
{},
...Array.from(arr, ([a, b]) => ({
year: a,
locationAndPrice: b
}))
);
const obj = Object.fromEntries(values);
return obj;
});
这是从上述函数返回的console.log,与预期的一样
{
locationAndPrice:
{
Barking & Dagenham: "50460"
Barnet: "93285"
Bexley: "64958"
Brent: "71307"
Bromley: "81671"
Camden: "120933"
City of London: "91449"
Croydon: "69158"
Ealing: "79886"
Enfield: "72515"
Greenwich: "62300"
Hackney: "61297"
Hammersmith & Fulham: "124903"
Haringey: "76288"
Harrow: "84770"
Havering: "68000"
Hillingdon: "73835"
Hounslow: "72232"
Islington: "92516"
Kensington & Chelsea: "182695"
Kingston upon Thames: "80876"
Lambeth: "67771"
Lewisham: "60491"
Merton: "82071"
Newham: "53539"
Redbridge: "72190"
Richmond upon Thames: "109326"
Southwark: "67885"
Sutton: "71537"
Tower Hamlets: "59865"
Waltham Forest: "61319"
Wandsworth: "88559"
Westminster: "133025,"}
year:{ "Jan-95"}
}
}
我想将上面的JSON数据转换成这种形式
{
"location": "City of London",
"price": "93285",
},
我现在得到的是
{
"location": "Westminster",
"price": "406413",
},
现在的问题是,“威斯敏斯特”
作为数据迭代的位置被重复。价格是迭代且唯一的
请提出问题,以便我可以细化此问题并得出结果。更新:
最后我自己弄明白了。与本问题上半部分提到的数据相同
import React, { Component } from "react";
import data from "./data/convertedData/POSTCODES FOR DISTRICT_AVRG HOUSE PRICE - Average price.json";
class JSONCreator extends Component {
componentDidMount() {
this.doSomething();
}
doSomething = () => {
const newData = data.map(i => {
const j = i.year;
const doStuff = Object.entries(j).map(([key, value]) => [key, value]);
const entries = doStuff.map(a => ({
year: a[0],
locationAndPrice: a[1]
}));
return entries;
});
const newState = newData.map(i => {
const j = i[0].year;
return Object.entries(i[0].locationAndPrice).map(a => ({
year: j,
location: a[0],
price: a[1]
}));
});
console.log("newData", newState);
this.setState({
newState: newState
})
};
render() {
console.log("this.state", this.state);
return <div>{JSON.stringify(this.state)}</div>;
}
}
export default JSONCreator;
import React,{Component}来自“React”;
从“/data/convertedData/POSTCODES FOR DISTRICT_AVRG HOUSE PRICE-Average PRICE.json”导入数据;
类JSONCreator扩展组件{
componentDidMount(){
这个。doSomething();
}
doSomething=()=>{
const newData=data.map(i=>{
常数j=一年;
const doStuff=Object.entries(j.map)([key,value])=>[key,value]);
const entries=doStuff.map(a=>({
年份:a[0],
地点和价格:a[1]
}));
返回条目;
});
const newState=newData.map(i=>{
常数j=i[0]。年;
return Object.entries(i[0].locationAndPrice.map(a=>({
年份:j,
地点:a[0],
价格:a[1]
}));
});
console.log(“newData”,newState);
这是我的国家({
新闻状态:新闻状态
})
};
render(){
log(“this.state”,this.state);
返回{JSON.stringify(this.state)};
}
}
导出默认JSONCreator;
请单击然后单击[]
代码段编辑器并生成一个-目前很难理解您的代码。覆盖代码肯定不是一个好主意。实际上,这两个函数都不起作用<代码>对象。赋值({},{a:1,b:2},{a:3,b:4},{a:11,b:12})将始终以{a:11,b:12}
结束@mplungjan I willnow@Bergi好的,没问题。我会调查的。当我推出可复制的代码时,最好能找到替代方案。我对使用Object.methods还不熟悉,所以我一直在从其他堆栈溢出的答案中寻找答案,希望能很快找到一个解决方案。
{
"location": "Westminster",
"price": "406413",
},
import React, { Component } from "react";
import data from "./data/convertedData/POSTCODES FOR DISTRICT_AVRG HOUSE PRICE - Average price.json";
class JSONCreator extends Component {
componentDidMount() {
this.doSomething();
}
doSomething = () => {
const newData = data.map(i => {
const j = i.year;
const doStuff = Object.entries(j).map(([key, value]) => [key, value]);
const entries = doStuff.map(a => ({
year: a[0],
locationAndPrice: a[1]
}));
return entries;
});
const newState = newData.map(i => {
const j = i[0].year;
return Object.entries(i[0].locationAndPrice).map(a => ({
year: j,
location: a[0],
price: a[1]
}));
});
console.log("newData", newState);
this.setState({
newState: newState
})
};
render() {
console.log("this.state", this.state);
return <div>{JSON.stringify(this.state)}</div>;
}
}
export default JSONCreator;