简体   繁体   English

我正在尝试从数组内的对象中推送值

[英]I am trying to push values from an object inside an array

I am trying to push data coming from an array of objects (key/value), then pushing it inside an array, to display them later. 我正在尝试推送来自对象(键/值)数组的数据,然后将其推送到数组中,以便稍后显示它们。

I have already tried doing the push inside the array but till now it is always giving undefined for the total amount of items being pushed. 我已经尝试过在数组内部进行推送,但到目前为止,对于推送的项目总数,它始终未定义。

//array
var clients_array = '[{"code": "1", "name": "client 1"}, 
{"code": "2", "name": "client 2"}, {"code": "3", "name": "client 3"}]';

var displayClient = [];

var json_parse1 = JSON.parse(clients_array);
json_parse1.forEach(function(client){
    console.log(client.code + ' - ' + client.name);
    displayClient.push(client.code + ' - ' + client.name);
    alert(displayClient[i]);
})

Using the above code, In the console.log I am getting the correct data which I would like to push. 使用上面的代码,在console.log中,我获得了想要推送的正确数据。 But when alerting the displayClient array, which in this case should be filled with the console.logged data, it displays undefined for each data. 但是,当警告displayClient数组(在这种情况下应使用console.logged数据填充)时,它会为每个数据显示未定义。 Hence, I would like to push the correct data as displayed in the console.log. 因此,我想推送console.log中显示的正确数据。

You need to add a parameter for i in the callback function. 您需要在回调函数中为i添加一个参数。

json_parse1.forEach(function (client, i) {
                                      ^

 var clients_array = '[{"code": "1", "name": "client 1"}, {"code": "2", "name": "client 2"}, {"code": "3", "name": "client 3"}]'; var displayClient = []; var json_parse1 = JSON.parse(clients_array); json_parse1.forEach(function (client, i) { console.log(client.code + ' - ' + client.name); displayClient.push(client.code + ' - ' + client.name); alert(displayClient[i]); }) console.log(displayClient); 

A better approach would be to use Array#map and return the new items. 更好的方法是使用Array#map并返回新项目。

 var array = [{ code: "1", name: "client 1" }, { code: "2", name: "client 2" }, { code: "3", name: "client 3" }], displayClient = array.map(({ code, name }) => [code, name].join(' - ')); console.log(displayClient); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试将对象中的一些值推送到 3 维数组中 - I am trying to push some values from an object into a 3 dimensional array 我正在尝试使用.push() 将值推送到数组中,但是一旦数组长度达到两个,它就会停止推送值 - I am trying to push a value onto an array with .push(), but it stops pushing values once the array length reaches two 我试图在向该 object 添加新属性之前将 object 推送到数组,但它直接更新数组 - i am trying to push an object to an array before adding a new property to that object , but it directly updates the array 我正在尝试从对象数组中检索数字数组 - I am trying to retrieve an array of numbers from an array of object 当我使用该方法时尝试将一些值推入空数组中,但由于某种原因,数组的值被覆盖 - trying to push some values inside of an empty array when I use the method, but for some reason, the value of the array is overwriting 我正在尝试使用 .push() 来保存多个值,但我得到了“{}” - I am trying to use .push() to save multiple values but I got '{}' 我试图写一个从数组创建一个简单的对象。 在我的数组中,我存储了变量: - I am trying to write a create a simple object from an array. In my array, I have variable stored: 我正在尝试使用 images.innerHTML = “&lt;&gt;” 从数组中的 object 中提取图像,但我很难弄清楚 - I am trying to pull images from object in an array using images.innerHTML = “<>” and I am having hard time trying to figure it out 从对象获取值并推入数组javascript - get values from object and push into array javascript 将所有值从对象推送到数组? - Push all values from object to array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM