显示数组的整个键/值
display entire key/value of arrays
我试图打印出以下数组:
- 第一个是单个数组
-
下面是存储在列表
中的两个数组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>
相关文章:
- json数组显示在<李>使用angular js
- AngularJS错误处理:根据错误数组显示错误
- 在 MVC3 中将字节数组显示为图像
- Highcharts使用数组显示每个气泡点的附加信息
- 如何使用数组显示文本
- 对于数组 {{显示项目}} 中的每个项目
- 将 PHP 数组显示为动画
- 如何将多维数组显示为表格
- MongoDb - 具有空值的有界数组显示无界
- 如何在谷歌地图中使用地址数组显示信息窗口和折线
- 如何从查询中将json数组显示为重复区域
- 根据布尔数组显示或隐藏jQuery元素
- 初学者javascript;for循环,对象数组显示为未定义
- JavaScript数组显示10个图像,然后显示下一个10个
- JS数组显示不正确
- Meteor:如何在html页面上使用光标数组显示列表
- Javascript发布数组显示语法错误
- PHP数组到JavaScript数组显示[object,object]
- 如何使用数组显示循环中的文本
- Javascript数组显示