显示数组的整个键/值

display entire key/value of arrays

本文关键字:数组 显示      更新时间:2023-09-26

我试图打印出以下数组:

  • 第一个是单个数组
  • 下面是存储在列表

    中的两个数组
    var ideaGeneration_online_stagea = {
        title: "Entrepreneurial Behaviour",
        outcome: "Open University, free course about entrepreneurship, it's nature and functions and to help you establish a business idea.",
        cost: "",
        duration: "",
        link: "http://www.open.edu/openlearn/money-management/management/business-studies/entrepreneurial-behaviour/content-section-0"
    }
    var ideaGeneration_online_stageb = [
    {
        title: "The serial entrepreneur's guide to startup ideas",
        outcome: "",
        cost:  10.00 ,
        duration: 6,
        link: "https://www.udemy.com/the-step-by-step-guide-to-creating-100mm-startup-ideas/"
    },
    {
        title: "21 critical lessons for entrepreneurs",
        outcome: "",
        cost: "",
        duration: 2,
        link: "https://www.udemy.com/21-golden-rules-for-entrepreneurs/"
    }
    ];
    alert(ideaGeneration_online_stagea);
    alert(ideaGeneration_online_stageb);
    

我知道我使用alert输出它的方式是不正确的,我得到的结果是:[object Object]为第一个,[object Object], [object Object]为第二个,所以我显然没有正确显示它们。

使用console.log(ideaGeneration_online_stagea)而不是警报,并检查您的(chrome)开发人员工具>控制台选项卡

因此,根据您的评论,我假设您想将所有标题值组合在一个字符串中并在页面中显示它。如果是,请按照下面的

添加一个HTML标签用于显示输出

<div id="output"></div>

添加以下javascript代码

    var outputString="";
    if(ideaGeneration_online_stagea.length>0) {
       ideaGeneration_online_stagea.forEach(function(item) {
          outputString = outputString + item.title +"<br>"; 
       });
    } else {
        outputString = ideaGeneration_online_stagea.title;
    }
    document.getElementById("output").innerHTML=outputString;

使用以下代码

alert(JSON.stringify(ideaGeneration_online_stagea));
alert(JSON.stringify(ideaGeneration_online_stageb ));

使用它,让我知道它是否有效

@user3907211如果json只有一个对象,那么你可以直接调用它的属性例如

ideaGeneration_online_stagea.title

否则使用forEach

如果您不确定数组的大小,请使用长度检查器

示例

if(ideaGeneration_online_stagea.length>0) {
ideaGeneration_online_stagea.forEach(function(item) {
   alert(item.title); 
});
} else {
    alert(ideaGeneration_online_stagea.title);
}

如您所愿!

你想要基于key title

之类的东西来提醒json

在这里显示了多种方式!

alert(JSON.stringify(ideaGeneration_online_stagea));
alert(JSON.stringify(ideaGeneration_online_stageb ));

它将返回所有json作为字符串!!


var temp = [ideaGeneration_online_stagea,ideaGeneration_online_stageb];
$.each(temp, function(i, val){
    if(val.length){
      for(i=0;i<val.length;i++){        
        $.each(val[i], function(i, value){
            value != ''? alert(value):false;
        });
      }
    }else{
      $.each(val, function(i, value){
            value != ''? alert(value):false;
        });
    }  
});

将你的输入组合成一个数组,然后分割,它将返回非空值

  var ideaGeneration_online_stagea = {
title: "Entrepreneurial Behaviour",
outcome: "Open University, free course about entrepreneurship, it's nature and functions and to help you establish a business idea.",
cost: "",
duration: "",
link: "http://www.open.edu/openlearn/money-management/management/business-studies/entrepreneurial-behaviour/content-section-0"
}
var ideaGeneration_online_stageb = [
{
title: "The serial entrepreneur's guide to startup ideas",
outcome: "",
cost:  10.00 ,
duration: 6,
link: "https://www.udemy.com/the-step-by-step-guide-to-creating-100mm-startup-ideas/"
},
{
title: "21 critical lessons for entrepreneurs",
outcome: "",
cost: "",
duration: 2,
link: "https://www.udemy.com/21-golden-rules-for-entrepreneurs/"
}
];
var temp = [ideaGeneration_online_stagea,ideaGeneration_online_stageb];
$.each(temp, function(i, val){
if(val.length){
  for(i=0;i<val.length;i++){        
    $.each(val[i], function(i, value){
        value != ''? alert(value):false;
    });
  }
}else{
  $.each(val, function(i, value){
        value != ''? alert(value):false;
    });
}  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


它将只返回title

var temp = [ideaGeneration_online_stagea,ideaGeneration_online_stageb];
$.each(temp, function(i, val){
    if(val.length){
      for(i=0;i<val.length;i++){        
        alert(val[i].title);
      }
    }else{
      alert(val.title);
    }  
});

  var ideaGeneration_online_stagea = {
title: "Entrepreneurial Behaviour",
outcome: "Open University, free course about entrepreneurship, it's nature and functions and to help you establish a business idea.",
cost: "",
duration: "",
link: "http://www.open.edu/openlearn/money-management/management/business-studies/entrepreneurial-behaviour/content-section-0"
}
var ideaGeneration_online_stageb = [
{
title: "The serial entrepreneur's guide to startup ideas",
outcome: "",
cost:  10.00 ,
duration: 6,
link: "https://www.udemy.com/the-step-by-step-guide-to-creating-100mm-startup-ideas/"
},
{
title: "21 critical lessons for entrepreneurs",
outcome: "",
cost: "",
duration: 2,
link: "https://www.udemy.com/21-golden-rules-for-entrepreneurs/"
}
];
var temp = [ideaGeneration_online_stagea,ideaGeneration_online_stageb];
$.each(temp, function(i, val){
if(val.length){
  for(i=0;i<val.length;i++){        
    alert(val[i].title);
  }
}else{
  alert(val.title);
}  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>