我需要获取值和轴。 这就是我所拥有的。
var d = [
[
{axis:"Customer Service",value:0.6},
{axis:"Data",value:0.6},
{axis:"Network",value:0.6},
{axis:"Product",value:0.6},
{axis:"Specialized",value:0.6},
{axis:"Threat",value:0.6},
],
[
{axis:"Customer Service",value:0.3},
{axis:"Data",value:0.3},
{axis:"Network",value:0.3},
{axis:"Product",value:0.3},
{axis:"Specialized",value:0.3},
{axis:"Threat",value:0.3},
]
function myFunction() {
var sample = d[0];
alert(sample);
有没有一种方法可以获取输出 客户服务:0.6
答案 0 :(得分:1)
查看复合数据结构时,方括号将代表数组和大括号对象。在这种情况下,存在一个包含对象的数组数组,并且需要通过对象的“轴”来标识它们。
var d = [
[
{axis:"Customer Service",value:0.6},
{axis:"Data",value:0.6},
{axis:"Network",value:0.6},
{axis:"Product",value:0.6},
{axis:"Specialized",value:0.6},
{axis:"Threat",value:0.6},
],
[
{axis:"Customer Service",value:0.3},
{axis:"Data",value:0.3},
{axis:"Network",value:0.3},
{axis:"Product",value:0.3},
{axis:"Specialized",value:0.3},
{axis:"Threat",value:0.3},
]
];
function myFunction() {
let sample = '';
let index = 0; // which of the array items is being looked at, if in a loop
// this value could be incremented to get the Customer Service value from
// all of the entries in the outer array.
d[index].forEach(function(dItem) { // loop through the array of a single object
// looping through allows you to access any of the axis objects, not just the first
if (dItem.axis === 'Customer Service') { // check which object
sample = dItem.value; // if the desired object then set sample
}
});
// alternatively check to see that sample is still not equal to '' before
// alerting
alert(sample);
}
myFunction();
答案 1 :(得分:0)
选择所需的对象,然后alert
值:
var d = [
[
{axis:"Customer Service",value:0.6},
{axis:"Data",value:0.6},
{axis:"Network",value:0.6},
{axis:"Product",value:0.6},
{axis:"Specialized",value:0.6},
{axis:"Threat",value:0.6},
],
[
{axis:"Customer Service",value:0.3},
{axis:"Data",value:0.3},
{axis:"Network",value:0.3},
{axis:"Product",value:0.3},
{axis:"Specialized",value:0.3},
{axis:"Threat",value:0.3},
]];
var obj = d[0][0];
alert(obj.axis + ": " + obj.value);
答案 2 :(得分:0)
您的解决方案已经结束,您只需要添加右括号并指定正确的索引即可。尝试在浏览器中运行此代码。
var d = [
[
{axis:"Customer Service",value:0.6},
{axis:"Data",value:0.6},
{axis:"Network",value:0.6},
{axis:"Product",value:0.6},
{axis:"Specialized",value:0.6},
{axis:"Threat",value:0.6},
],
[
{axis:"Customer Service",value:0.3},
{axis:"Data",value:0.3},
{axis:"Network",value:0.3},
{axis:"Product",value:0.3},
{axis:"Specialized",value:0.3},
{axis:"Threat",value:0.3},
]];
function myFunction() {
var sample = d[0][0];
alert(sample.axis + ": " + sample.value);
}
myFunction();
答案 3 :(得分:0)
这是获得客户服务输出的方式:0.6。 (ES6样式)
var d = [
[
{axis:"Customer Service",value:0.6},
{axis:"Data",value:0.6},
{axis:"Network",value:0.6},
{axis:"Product",value:0.6},
{axis:"Specialized",value:0.6},
{axis:"Threat",value:0.6},
],
[
{axis:"Customer Service",value:0.3},
{axis:"Data",value:0.3},
{axis:"Network",value:0.3},
{axis:"Product",value:0.3},
{axis:"Specialized",value:0.3},
{axis:"Threat",value:0.3},
]
];
const myFunction = () => {
let sample = ``;
let axis = ``;
let index = 0;
d[index].forEach(item => {
if (item.axis === `Customer Service`) {
axis = item.axis;
sample = item.value;
}
});
alert(`${axis}: ${sample}`); // Customer Service: 0.6
}
myFunction();