从我的 JSON 对象添加到我的列表数组

罗伯特·达菲

这就是我目前正在做的事情。我尝试将值从 my 推json入 links 数组,但在输入数组后,值变为undefined. 移动数据并使其仍然可用的更好方法是什么?

var links = [];
$.ajax({
  url: 'data.json',
  dataType: 'json',
  type: 'get',
  cache: false,
  success: function(data) {
    $(data.emails).each(function(index, value) {
      links.push(value);
    });
  }
});

//data.json contains:
{
  "emails": [{
    "source": "[email protected]",
    "target": "[email protected]"
  }]
}

使用此代码的结果是相同的。值进入数组,但是当您通过链接访问它们时。(值的名称)它返回“未定义”

 $.each(data.emails, function(index, value) { 
        links.push(value);
        console.log(links);
        return value;
    });

结果

沙希达拉

试试这个

<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>        
<script type="text/javascript">
//alert("data");
var links = [];
   $.ajax({
         url: 'test1.php',
         type: 'post',
         dataType: 'json',
         data: {

         }
       }).done(function(data) {
        $.each(data.emails, function(index, value) { 
        links.push(value);
        console.log(links);
        return value;
        });
    });

    function getVal(){
        console.log(links[0].target);
    }
</script>
<button onclick="getVal()">Get target</button>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

元素未添加到我的数组列表中

什么是$$ hashKey添加到我的JSON.stringify结果中

我应该将google-services.json(来自Firebase)添加到我的存储库中吗?

什么是原型,为什么将它们添加到我的json对象中

如何在Jax-rs中将JSON对象添加到我的JSON响应中?

将音频对象添加到我的反应状态数组

神秘的空值添加到我的数组的开头

JwtBuilder将\添加到我的Json字符串-Java

字母未添加到我的数组中

在Java中将“ Circular”添加到我的数组中

如何将localStorage添加到我的多语言网站/ Jquery / JSON

为什么不能将newtonsoft.Json.dll ref添加到我的项目中?

将整数值添加到我的数组

无法将对象添加到我的列表中

如何使用 JSON 和 PHP 在表单提交时将我的对象添加到数组中?

如何根据 JSON 文件的内容将 TextViews 动态添加到我的布局?

如何将文件输入流正确添加到我的 Firebase JSON 文件

你调用的对象是空的。当我将项目添加到我的数组列表中时

我有这个 json 响应....我想将它添加到我的 android 微调器?我如何添加这个

Typescript .push 函数将空对象添加到我的数组中

循环遍历 json 对象时,没有将键添加到我的 json 对象中

为什么 Newtonsoft.Json.JsonConvert.SerializeObject 将“_repository”添加到我列表中的每个项目?

我如何将 AWS env varaible 添加到我的包 json 中

需要帮助将对象添加到我的数组列表中,只要它发生变化

如何将 json 正文添加到我的 axios 发布请求中?

如何将 json 数据添加到我的 xcode 项目中?

("\r") 自动添加到我的 JSON 键

将数组添加到我的 ExecuteReader() 函数

一個名為“-”的節點包被無意中添加到我的 package.json 中?