Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将数组转换为具有键/值对的对象_Javascript_Arrays_Reactjs_Object - Fatal编程技术网

Javascript 将数组转换为具有键/值对的对象

Javascript 将数组转换为具有键/值对的对象,javascript,arrays,reactjs,object,Javascript,Arrays,Reactjs,Object,我仍在学习JavaScript,所以如果这似乎是一个补救问题,我提前道歉 我以数组的形式从API调用接收数据,如下所示: arr = ['topic1',497,'topic2',591,'topic3',17,'topic4',980] let x = arr.filter((element, index) => { return index % 2 === 0; filter elements located at an even index }); let y = arr.f

我仍在学习JavaScript,所以如果这似乎是一个补救问题,我提前道歉

我以数组的形式从API调用接收数据,如下所示:

arr = ['topic1',497,'topic2',591,'topic3',17,'topic4',980]
let x = arr.filter((element, index) => {
  return index % 2 === 0; filter elements located at an even index
});

let y = arr.filter((element, index) => {
  return index % 2 === 1; filter elements located at an odd index
});
虽然从该数组中不明显,但每个主题字符串后面的数字是前一个主题的计数

我希望在React应用程序中使用此数据,即将其映射到表或其他数据结构。但当这些值是匿名的时,这是非常困难的

所以,我想把这个数组转换成一个具有键/值对的对象。因此,在对数组执行某种循环/过滤器/映射等操作后,我需要以以下内容结束:

newArr = [
  {topic: 'topic1', count: 497},
  {topic: 'topic2', count: 591},
  {topic: 'topic3', count: 17},
  {topic: 'topic4', count: 980},
]
我尝试过很多技术,从使用字典、地图、地图、过滤器、forEach,到。。。在这一点上,我甚至记不起来还有更多的技巧。是的,我搜索了很多网页和论坛。问题是,我的问题包含了JavaScript的一些构建块,因此我发现的答案对于我的问题永远都不够具体

我成功地过滤掉了数组中的偶数和奇数元素,如下所示:

arr = ['topic1',497,'topic2',591,'topic3',17,'topic4',980]
let x = arr.filter((element, index) => {
  return index % 2 === 0; filter elements located at an even index
});

let y = arr.filter((element, index) => {
  return index % 2 === 1; filter elements located at an odd index
});
但我从来没有成功地得到我需要的结果。谁能帮我指出正确的方向吗


我正在使用React,所以请随意推荐现代JS技术。谢谢

您需要循环API响应,并从数组中构建对象

let objectArray = []

for(let i = 0; i < arr.length - 1; i+=2){
    objectArray.push({"topic": arr[i], "count": arr[i+1]})
}
let objectArray=[]
对于(设i=0;i
我相信有一种更简单的方法,但这会满足你的需要


在JSFIDLE上查看它的操作:

您可以使用
array.reduce
来获取数组并发出聚合值、数组、对象或其他内容。全面实施将是:

const newArr = arr.reduce((fonts, value, index) => {
   if (0 === index % 2) {
     fonts.push({ topic: value });
   } else {
     // output array will be half the size of the input hence the / 2
     fonts[Math.ceil(index / 2 - 1)].count = value;
   }

   return fonts;
}, []);
如果你愿意使用像
lodash
这样的外部库,你可以让这更容易,因为它会为你分块数组

const newArr = _.zipWith(_.chunk(arr, 2), a => ({ topic: a[0], count: a[1] }));

从你的问题来看,arr是一个字符串,数字…模式,我觉得你可以这样做

const arr = ['topic1',497,'topic2',591,'topic3',17,'topic4',980];
const result = [];

for(let i=0; i<arr.length; i+=1) {
 typeof arr[i] === 'string' 
 ?
  result.push({topic: arr[i], count: arr[i+1]})
:
null;
const arr=['topic1',497,'topic2',591,'topic3',17,'topic4',980];
常量结果=[];

对于(让i=0;i因为您是新用户,所以给您一个有用的建议:数组和对象在许多语言中使用。因此,请在您的问题中添加一个适当的语言标记(我在这一次为您做了)。如果两个数组相等,循环它们并从中创建一个对象并添加到一个数组中!可能重复感谢,reporter…很高兴知道!Bharadwaj,我想我明白了,但是只有一个数组由topic、count、topic、count等组成。因此,每对项目之间都有关联。通过组合两个过滤数组,本周是的,我能确保生成的对象以正确的顺序保持正确的关系吗?GalAbra,如果它是重复的,我无法理解其他帖子的头绪。:)在这个例子中,我要做的是,我从API返回的数组结构永远不会改变……只有数据会改变。因此,我需要某种函数来获取数据,不管数组是长是短,并根据具体情况将每个元素映射到“topic”或“count”。我从不需要动态添加或删除属性。我调用,获取数组,执行转换。然后我有一个很好的对象,我可以映射到一张桌子或任何我想要的。非常感谢!!这正是我想做的。这真是太简单了!我有很多东西要学…上面的“for-loop”解决方案实际上可以满足我的需要。不过,谢谢你的帮助!