Mapping-独立的JavaScript对象,然后追加的最后一个对象对象、独立、Mapping、JavaScript

2023-09-11 06:53:30 作者:不曾看到我心慌

有一个很有点挑战,处理好以下几个问题。

There is a quite bit challenge to handle the following problem.

我有以下的数组和JavaScript对象:

I have the following array and javascript objects:

输入:

我有三个不同颜色的(蓝,绿,黄)和三种不同大小的(SZ = 9,11,13)中的JavaScript对象,并试图将其映射。

I have three different colors (blue,green,yellow) and three different sizes (sz=9,11,13) in the javascript objects and trying to map them.

color=["blue","green","yellow"];

myObj=[{x:0,y:1,sz:9},{x:4,y:11,sz:9},{x:11,y:17,sz:11},{x:29,y:18,sz:13}];

输出:

data1:[{x:0,y:1,sz:9,color:"blue"},{x:4,y:11,sz:9,color:"blue"}];
data2:[{x:4,y:11,sz:9,color:"green"},{x:11,y:17,sz:11,color:"green"}];
data3:[{x:11,y:17,sz:11,color:"yellow"},{x:29,y:18,sz:13,color:"yellow"}];

下面是的jsfiddle 这基于他们地图对象SZ 属性,并增加了颜色属性。丢失的唯一的事情是添加的最后一个对象,并改变颜色。

Here is the jsfiddle which map objects based on their sz property and adds color properties. The only thing is missing is to add the last object and change color.

例如:

数据2 的第一个对象是来自数据1 的只是不同的颜色是绿色的最后一个对象。

data2's first object comes from data1's last object except different color which is green.

下面是我:

数据2:[{X:11 Y:17,SZ:11,颜色:绿色}];

下面是我需要的:

数据2:[{X:4,Y:11,SZ:9,色彩:绿色},{X:11 Y:17,SZ:11,颜色:绿色 }];

推荐答案

我想你需要这样的事情:

I think you need something like that:

抱歉的是,我删除了旧的code。

Sorry for that, i deleted the old code.

更新3:

https://jsfiddle.net/kxhsapad/3/

旧请求:

var size = [9, 11, 13, 15];
var colorA = ["blue", "green", "yellow", "red"];

var myObj = [{x:0,y:1,sz:9},{x:4,y:11,sz:9},{x:11,y:17,sz:11},{x:29,y:18,sz:13},{x:39,y:15,sz:15}];
var d1 = [];
var d2 = [];
for (var i = 0; i < size.length; i++) {

    d1 = [];  
    d1[0] = myObj[i];
    d1[0].color = colorA[i];
    d1[1] = myObj[i + 1];
    d1[1].color = colorA[i];
    d2.push(d1);
}
for (var i = 0; i < d2.length; i++) {
    for (var j = 0; j < d2[i].length; j++) {
        var html = '<div id="' + i + '_' + j + '"';
        if (j%2==0) html += ' style="float:left;"';
        html += '>x=' + d2[i][j].x + ';y=' + d2[i][j].y + ';sz=' + d2[i][j].sz + ';color=' + d2[i][j].color + ';</div>';
        $('#container').append(html);
    }
}

请检查的jsfiddle全code https://jsfiddle.net/kxhsapad/

Please check jsfiddle for full code https://jsfiddle.net/kxhsapad/