Javascript 如何正确地将空映射设置为已解析的json(这是一个新映射)?

Javascript 如何正确地将空映射设置为已解析的json(这是一个新映射)?,javascript,arrays,reactjs,fetch,Javascript,Arrays,Reactjs,Fetch,我已经声明了这个空映射mapA={},但是由于某种原因,当我将它设置为已解析的jsonmapA=parsedJSON时,将导致相同或重复键的错误 我获取的json类似于这样的东西,[{a:1},{a:2},{a:3}],只是为了澄清一下……这是一个json,对吗?因为它也是一种数组,对吗 但是我获取了一个json字符串,然后解析它,然后我收到了我提到的错误 然而,mapA=[{a:1},{a:2},{a:3}]对我来说很有效,但这是硬编码的。这里到底哪里不对 EDIT1: 这是我使用的一个示例数

我已经声明了这个空映射
mapA={}
,但是由于某种原因,当我将它设置为已解析的json
mapA=parsedJSON
时,将导致相同或重复键的错误

我获取的json类似于这样的东西,
[{a:1},{a:2},{a:3}]
,只是为了澄清一下……这是一个json,对吗?因为它也是一种数组,对吗

但是我获取了一个json字符串,然后解析它,然后我收到了我提到的错误

然而,
mapA=[{a:1},{a:2},{a:3}]
对我来说很有效,但这是硬编码的。这里到底哪里不对

EDIT1

这是我使用的一个示例数据

[{"location_id":1,"flight_location":"KUL","description":"Kuala Lumpur","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":2,"flight_location":"KIX","description":"Osaka-Kansai","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":3,"flight_location":"PEK","description":"Beijing","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":4,"flight_location":"HGH","description":"Hangzhou","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":5,"flight_location":"PVG","description":"Shanghai","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":6,"flight_location":"SZX","description":"Shenzhen","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":7,"flight_location":"DPS","description":"Bali","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":8,"flight_location":"CGK","description":"Jakarta","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":9,"flight_location":"DMK","description":"Bangkok","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0}]
这是我的密码:

let fakeData = [{"location_id":1,"flight_location":"KUL","description":"Kuala Lumpur","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":2,"flight_location":"KIX","description":"Osaka-Kansai","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":3,"flight_location":"PEK","description":"Beijing","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":4,"flight_location":"HGH","description":"Hangzhou","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":5,"flight_location":"PVG","description":"Shanghai","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":6,"flight_location":"SZX","description":"Shenzhen","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":7,"flight_location":"DPS","description":"Bali","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":8,"flight_location":"CGK","description":"Jakarta","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":9,"flight_location":"DMK","description":"Bangkok","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0}];

let flights = {};


fetch(link) //link is the api link which I use to fetch the data
    .then(function(response) {
      return response.json(); //In here I parse it
    }).then(function(json) {
        /*flights = fakeData;     Causes same error. Run this only to test when api is down */  
        flights = json; // Same error. Set our flights map to the parsed json which is the map     
    }).catch(function(ex) {
      console.log('parsing failed', ex);
    });
控制台输出:

这是来自React库的。顺便说一句,我不太熟悉他们库中的代码

我的组件:

飞行列表

const FlightList = ({flights, deleteFlight}) => {
    return (
        <table className="table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Location</th>
                    <th>Description</th>
                </tr>
            </thead>
            <tbody>
                {flights.map(flight =>
                    <FlightListRow key={flight.location_id} flight={flight}/>
                )}
            </tbody>
        </table>
    );
};
const FlightList=({flights,deletefights})=>{
返回(
身份证件
位置
描述
{flights.map(flights=>
)}
);
};
FlightListRow

const FlightListRow = ({flight}) => {
    return (
        <tr>
            <td><Link to={'/editflight/' + flight.location_id}>{flight.location_id}</Link></td>
            <td><Link to={'/editflight/' + flight.location_id}>{flight.flight_location}</Link></td>
            <td><Link to={'/editflight/' + flight.location_id}>{flight.description}</Link></td>
        </tr>
    );
};
const FlightListRow=({flightlist})=>{
返回(
{flight.location_id}
{航班。航班位置}
{flight.description}
);
};

这是一个react错误,这意味着在react组件中,您有一个组件数组,并且在两个或多个组件上使用相同的键

       {flights.map((flight,index) =>
            <FlightListRow key={index} flight={flight}/>
        )}
{flights.map((航班,索引)=>
)}

一个有效的
JSON
将属性sor四舍五入为以下引号:
[{“A”:1},{“A”:2},{“A”:3}]
。所以解析失败是正常的。要检查你的“JSON”的有效性,你可以使用这样一个网站:@ADreNaLiNe DJ:它如何处理重复的密钥?我的JSON是有效的,我刚刚检查过。因此,我将其设置为我的
mapA
,但仍然得到相同或重复的键错误。这是我的结果:请将文本形式的相关数据添加到问题中。@NinaScholz我添加了数据我添加了我使用的组件。我的错误来自哪一部分?我确信位置号一定是重复的。你可以替换它。我已经试过你的建议了。出现一个错误,说明“索引”未定义。请参阅更新的代码,您需要它将索引添加到映射函数tooThanks中,它正常工作。我头痛了好几天,我记得一个10万人的代表一直问我太多问题,他否决了我的老问题,因为他希望我什么都知道。我很高兴你帮我解决了这个问题。再次感谢:)