Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在json数组中的每个json对象中添加另一个json对象_Javascript_Arrays_Json_Highcharts - Fatal编程技术网

Javascript 在json数组中的每个json对象中添加另一个json对象

Javascript 在json数组中的每个json对象中添加另一个json对象,javascript,arrays,json,highcharts,Javascript,Arrays,Json,Highcharts,嗨,我是Highchart、javascript、jquery、html、json数组、json对象的新手。我正在尝试向json数组中的每个json对象添加另一个json对象。在javascript中使用push可以这样做吗?任何人都可以指导我我会非常感激谢谢 包含json对象的当前jsonArray [{showInLegend: false, type: 'column', name: 'Test1', id: 'Test1', data: [10, 10] },{s

嗨,我是Highchart、javascript、jquery、html、json数组、json对象的新手。我正在尝试向json数组中的每个json对象添加另一个json对象。在javascript中使用push可以这样做吗?任何人都可以指导我我会非常感激谢谢

包含json对象的当前jsonArray

[{showInLegend: false, type: 'column',
    name: 'Test1',
    id: 'Test1',
    data: [10, 10]
},{showInLegend: false, type: 'column',
    name: 'Test2',
    id: 'Test2',
    data: [120, 120]
},{showInLegend: false, type: 'column',
    name: 'Test3',
    id: 'Test3',
    data: [320, 120]
}]
我想在jsonarray中的每个json对象中添加这个

tooltip: {
    pointFormatter: function () {
        return this.series.name + '<br/><span style="color:#273c75"><b> Amount : </b> </span>$' + this.y.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }
}
工具提示:{
pointFormatter:函数(){
返回this.series.name+'
金额:$'+this.y.toFixed(2).toString()。替换(/\B(?=(\d{3})+(?!\d))/g,“,”; } }
预期产量

[{showInLegend: false, type: 'column',
    name: 'Test1',
    id: 'Test1',
    data: [10, 10],tooltip: {
        pointFormatter: function () {
            return this.series.name + '<br/><span style="color:#273c75"><b> Amount : </b> </span>$' + this.y.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        }
    }
},{showInLegend: false, type: 'column',
    name: 'Test2',
    id: 'Test2',
    data: [120, 120],tooltip: {
        pointFormatter: function () {
            return this.series.name + '<br/><span style="color:#273c75"><b> Amount : </b> </span>$' + this.y.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        }
    }
},{showInLegend: false, type: 'column',
    name: 'Test3',
    id: 'Test3',
    data: [320, 120],tooltip: {
        pointFormatter: function () {
            return this.series.name + '<br/><span style="color:#273c75"><b> Amount : </b> </span>$' + this.y.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        }
    }
}]
[{showInLegend:false,键入:“column”,
名称:“Test1”,
id:'Test1',
数据:[10,10],工具提示:{
pointFormatter:函数(){
返回this.series.name+'
金额:$'+this.y.toFixed(2).toString()。替换(/\B(?=(\d{3})+(?!\d))/g,“,”; } } },{showInLegend:false,键入:“column”, 名称:“Test2”, id:'Test2', 数据:[120120],工具提示:{ pointFormatter:函数(){ 返回this.series.name+'
金额:$'+this.y.toFixed(2).toString()。替换(/\B(?=(\d{3})+(?!\d))/g,“,”; } } },{showInLegend:false,键入:“column”, 名称:“Test3”, id:'Test3', 数据:[320120],工具提示:{ pointFormatter:函数(){ 返回this.series.name+'
金额:$'+this.y.toFixed(2).toString()。替换(/\B(?=(\d{3})+(?!\d))/g,“,”; } } }]
如果这是
JSON
,首先执行
JSON.parse(data)
并获得正常的js数组,然后 使用javascript函数。示例代码

const exampleArray=JSON.parse(数据);
const result=exampleArray.map(项=>{
返回{
…项目,
工具提示:{
pointFormatter:函数(){
返回this.series.name+'
金额:$'+ this.y.toFixed(2.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); } } }; })
好吧,假设您的对象数组被称为
array
,您只需执行以下操作:

var tooltip = {
   pointFormatter: function () {
       return this.series.name + '<br/><span style="color:#273c75"><b> Amount : </b> </span>$' + this.y.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
   }
}

array.forEach(function(item) {
    item.tooltip = tooltip;
});
var工具提示={
pointFormatter:函数(){
返回this.series.name+'
金额:$'+this.y.toFixed(2).toString()。替换(/\B(?=(\d{3})+(?!\d))/g,“,”; } } array.forEach(函数(项){ item.tooltip=工具提示; });
这样,数组中的每个对象都将被修改。但是,请注意,我不知道如何使用
pointFormatter
函数,但除非使用
apply
call
调用它,否则它内部的
this
变量将是全局命名空间的别名,而不是特定对象


希望这有帮助。

您只需遍历数组并为每个条目定义一个属性即可。对对象中不存在的特性使用点运算符将创建一个。如果它存在,它将简单地覆盖它

以下是如何:

// 1. Define your list/array
var myList = [{showInLegend: false, type: 'column',
                name: 'Test1',
                id: 'Test1',
                data: [10, 10]
            },{showInLegend: false, type: 'column',
                name: 'Test2',
                id: 'Test2',
                data: [120, 120]
            },{showInLegend: false, type: 'column',
                name: 'Test3',
                id: 'Test3',
                data: [320, 120]
            }];
// 2. Traverse the list and define the value of the new property.
for (var i = 0; i < myList.length; i++) {

    myList[i].tooltip = function () {
                            console.log("insert my code here...");
                        }
}

// 3. Execute the function for the first list entry
this.myList[0].tooltip()
//1。定义您的列表/数组
var myList=[{showInLegend:false,键入:“column”,
名称:“Test1”,
id:'Test1',
数据:[10,10]
},{showInLegend:false,键入:“column”,
名称:“Test2”,
id:'Test2',
数据:[120120]
},{showInLegend:false,键入:“column”,
名称:“Test3”,
id:'Test3',
数据:[320120]
}];
// 2. 遍历列表并定义新特性的值。
对于(变量i=0;i
对于序列名称,我不确定该属性在哪里,但这可能就是您要查找的内容:

// 1. Define your list/array
var myList = [{showInLegend: false, type: 'column',
                name: 'Test1',
                id: 'Test1',
                data: [10, 10]
            },{showInLegend: false, type: 'column',
                name: 'Test2',
                id: 'Test2',
                data: [120, 120]
            },{showInLegend: false, type: 'column',
                name: 'Test3',
                id: 'Test3',
                data: [320, 120]
            }];
// 2. Traverse the list and define the value of the new property.
for (var i = 0; i < myList.length; i++) {

    myList[i].tooltip = function () {
                                return this.name + '<br/><span style="color:#273c75"><b> Amount : </b> </span>$' + this.data[0].toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");}
}

// 3. Execute the function for the first list entry
this.myList[0].tooltip()
//1。定义您的列表/数组
var myList=[{showInLegend:false,键入:“column”,
名称:“Test1”,
id:'Test1',
数据:[10,10]
},{showInLegend:false,键入:“column”,
名称:“Test2”,
id:'Test2',
数据:[120120]
},{showInLegend:false,键入:“column”,
名称:“Test3”,
id:'Test3',
数据:[320120]
}];
// 2. 遍历列表并定义新特性的值。
对于(变量i=0;i金额:$'+this.data[0].toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g,“,”;)
}
// 3. 为第一个列表条目执行函数
this.myList[0]。工具提示()
为什么不使用数组推送


您必须定义您想要的,并且您实际想要做的是修改数组的现有条目,而不是添加新条目。Push在数组末尾添加新元素/条目。因此,您实际上要做的是向条目/数组元素添加属性,而不是向列表中添加新元素。

下面是一个工作片段

const initData=[{showInLegend:false,键入:“column”,
名称:“Test1”,
id:'Test1',
数据:[10,10]
},{showInLegend:false,键入:“column”,
名称:“Test2”,
id:'Test2',
数据:[120120]
},{showInLegend:false,键入:“column”,
名称:“Test3”,
id:'Test3',
数据:[320120]
}];
const pointerObj=函数(){
返回this.series.name+'
金额:$'+this.y.toFixed(2).toString()。替换(/\B(?=(\d{3})+(?!\d))/g,“,”; }; const newData=initData.map(数据=>{ 返回{…数据,工具提示:{ 指针:函数(){ 返回this.series.name+'
金额:$'+this.y.toFixed(2).toString().replace(/