我想制作四个具有设置的宽度和高度的矩形。
var rects = {
key1: { x: base_x, y: base_y, fullname: "Seed", cnt: 3500, row: 1 },
key2: { x: 2*base_x, y: base_y, fullname: "Series A", cnt: 4700, row: 1 },
key3: { x: 3*base_x, y: base_y, fullname: "Series B", cnt: 2300, row: 1},
key4: { x: 4*base_x, y: base_y, fullname: "Series C", cnt: 1750, row: 1}}
我知道,如果我将{}
替换为[]
以将其变成一个数组,并且由于数组不能有键而放弃了键,那么我可以将它们变成矩形可以使用它。
var rectangles = svg.selectAll(".rect")
.data(rects)
.enter()
.append("rect")
稍后,我想在需要键的函数中使用rects
对象。
我想我可以像这样用rect创建一个数组
var rects_array = []
for (i = 0; i < d3.keys(rects).length; i++){
rects_array.push(rects[d3.keys(rects)[i]])
}
我只是好奇我是否可以直接传递rects
而不是通过它传递数组。
答案 0 :(得分:3)
简短答案:否,不能。
D3 int main ()
{
struct node {
int data;
struct node* next;
};
struct node* top=NULL;
int choice = 0;
printMenu ();
do
{
printf ("\nEnter a choice. ");
scanf ("%d", &choice);
switch (choice)
{
case 0:
{
exitProgram();
break;
}
case 1:
{
clrScreen();
break;
}
case 3:
{
pushpancake();
break;
}
default:
printf("\nInvalid Choice!\n");
}
}
while (choice != 0);
return 0;
}
void printMenu ()
{
printf ("0) Exit program.\n\n");
printf ("1) Clear Screen.\n\n");
printf ("2) Display the pancake stack.\n\n");
printf ("3) Push a pancake.\n\n");
printf ("4) Pop a pancake.\n\n");
}
void pushpancake()
{
char pancake[30];
int calories;
printf("Enter pancake name and its calories:");
scanf("%s %d,", pancake, &calories);
printf("The pancake has been added at the top of the stack.");
struct node *temp=(struct node*)malloc(sizeof(struct node));
strcpy(temp->pancake,pancake);
temp->data=calories;
temp->next=top;
top=temp;
}
方法仅接受三件事:
因此,您不能在此处使用对象。
关于最后一个代码片段,基于first()
对象创建数组的最简单方法是使用Object.values
:
library(tidyverse)
dat %>%
mutate(dist = abs(4-timepoint)) %>%
arrange(id, dist, timepoint) %>%
group_by(id) %>%
filter(timepoint %in% c(0, first(timepoint))) %>%
ungroup() %>%
arrange(id, timepoint)
或者,如果您想要D3方法,则可以使用d3.values
,该方法在内部使用data()
循环:
rects
var rects = {
key1: {
x: "foo",
y: "bar",
fullname: "Seed",
cnt: 3500,
row: 1
},
key2: {
x: "foo",
y: "bar",
fullname: "Series A",
cnt: 4700,
row: 1
},
key3: {
x: "foo",
y: "bar",
fullname: "Series B",
cnt: 2300,
row: 1
},
key4: {
x: "foo",
y: "bar",
fullname: "Series C",
cnt: 1750,
row: 1
}
};
console.log(Object.values(rects))
然后根据需要将数组传递到for...in
,并在其他地方使用var rects = {
key1: {
x: "foo",
y: "bar",
fullname: "Seed",
cnt: 3500,
row: 1
},
key2: {
x: "foo",
y: "bar",
fullname: "Series A",
cnt: 4700,
row: 1
},
key3: {
x: "foo",
y: "bar",
fullname: "Series B",
cnt: 2300,
row: 1
},
key4: {
x: "foo",
y: "bar",
fullname: "Series C",
cnt: 1750,
row: 1
}
};
console.log(d3.values(rects))
对象。